Update the address of the Free Software Foundation.
[wine.git] / tools / winedump / dump.c
blobe3124e957479dd22788492777a370bd9022f45f1
1 /*
2 * File dumping utility
4 * Copyright 2001,2005 Eric Pouech
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
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>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winedump.h"
47 #include "pe.h"
49 static void* dump_base;
50 static unsigned long dump_total_len;
52 void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
54 unsigned int i, j;
56 printf( "%s%08x: ", prefix, 0 );
57 if (!ptr)
59 printf("NULL\n");
60 return;
62 for (i = 0; i < size; i++)
64 printf( "%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ' );
65 if ((i % 16) == 15)
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 );
73 if (i % 16)
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] : '.' );
79 printf( "\n" );
82 const char* get_time_str(unsigned long _t)
84 time_t t = (time_t)_t;
85 const char *str = ctime(&t);
86 size_t len;
87 static char buf[128];
89 if (!str) /* not valid time */
91 strcpy(buf, "not valid time");
92 return buf;
95 len = strlen(str);
96 /* FIXME: I don't get the same values from MS' pedump running under Wine...
97 * I wonder if Wine isn't broken wrt to GMT settings...
99 if (len && str[len-1] == '\n') len--;
100 if (len >= sizeof(buf)) len = sizeof(buf) - 1;
101 memcpy( buf, str, len );
102 buf[len] = 0;
103 return buf;
106 unsigned int strlenW( const WCHAR *str )
108 const WCHAR *s = str;
109 while (*s) s++;
110 return s - str;
113 void dump_unicode_str( const WCHAR *str, int len )
115 if (len == -1) len = strlenW( str );
116 printf( "L\"");
117 while (len-- > 0 && *str)
119 WCHAR c = *str++;
120 switch (c)
122 case '\n': printf( "\\n" ); break;
123 case '\r': printf( "\\r" ); break;
124 case '\t': printf( "\\t" ); break;
125 case '"': printf( "\\\"" ); break;
126 case '\\': printf( "\\\\" ); break;
127 default:
128 if (c >= ' ' && c <= 126) putchar(c);
129 else printf( "\\u%04x",c);
132 printf( "\"" );
135 const void* PRD(unsigned long prd, unsigned long len)
137 return (prd + len > dump_total_len) ? NULL : (const char*)dump_base + prd;
140 unsigned long Offset(const void* ptr)
142 if (ptr < dump_base) {printf("<<<<<ptr below\n");return 0;}
143 if ((const char *)ptr >= (const char*)dump_base + dump_total_len) {printf("<<<<<ptr above\n");return 0;}
144 return (char*)ptr - (char*)dump_base;
147 static void do_dump( enum FileSig sig, const void* pmt )
149 if (sig == SIG_NE)
151 ne_dump( dump_base, dump_total_len );
152 return;
155 if (sig == SIG_LE)
157 le_dump( dump_base, dump_total_len );
158 return;
161 pe_dump(pmt);
164 static enum FileSig check_headers(void)
166 const WORD* pw;
167 const DWORD* pdw;
168 const IMAGE_DOS_HEADER* dh;
169 enum FileSig sig;
171 pw = PRD(0, sizeof(WORD));
172 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
174 switch (*pw)
176 case IMAGE_DOS_SIGNATURE:
177 sig = SIG_DOS;
178 dh = PRD(0, sizeof(IMAGE_DOS_HEADER));
179 if (dh)
181 /* the signature is the first DWORD */
182 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
183 if (pdw)
185 if (*pdw == IMAGE_NT_SIGNATURE)
187 sig = SIG_PE;
189 else if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE)
191 sig = SIG_NE;
193 else if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE)
195 sig = SIG_LE;
197 else
199 printf("No PE Signature found\n");
202 else
204 printf("Can't get the extented signature, aborting\n");
207 break;
208 case 0x4944: /* "DI" */
209 sig = SIG_DBG;
210 break;
211 case 0x444D: /* "MD" */
212 pdw = PRD(0, sizeof(DWORD));
213 if (pdw && *pdw == 0x504D444D) /* "MDMP" */
214 sig = SIG_MDMP;
215 else
216 sig = SIG_UNKNOWN;
217 break;
218 default:
219 printf("No known main signature (%.2s/%x), aborting\n", (char*)pw, *pw);
220 sig = SIG_UNKNOWN;
223 return sig;
226 int dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig)
228 int fd;
229 enum FileSig effective_sig;
230 int ret = 1;
231 struct stat s;
233 setbuf(stdout, NULL);
235 fd = open(name, O_RDONLY | O_BINARY);
236 if (fd == -1) fatal("Can't open file");
238 if (fstat(fd, &s) < 0) fatal("Can't get size");
239 dump_total_len = s.st_size;
241 #ifdef HAVE_MMAP
242 if ((dump_base = mmap(NULL, dump_total_len, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
243 #endif
245 if (!(dump_base = malloc( dump_total_len ))) fatal( "Out of memory" );
246 if ((unsigned long)read( fd, dump_base, dump_total_len ) != dump_total_len) fatal( "Cannot read file" );
249 effective_sig = check_headers();
251 if (effective_sig == SIG_UNKNOWN)
253 printf("Can't get a recognized file signature, aborting\n");
254 ret = 0;
256 else if (wanted_sig == SIG_UNKNOWN || wanted_sig == effective_sig)
258 switch (effective_sig)
260 case SIG_UNKNOWN: /* shouldn't happen... */
261 ret = 0; break;
262 case SIG_PE:
263 case SIG_NE:
264 case SIG_LE:
265 printf("Contents of \"%s\": %ld bytes\n\n", name, dump_total_len);
266 (*fn)(effective_sig, dump_base);
267 break;
268 case SIG_DBG:
269 dump_separate_dbg();
270 break;
271 case SIG_DOS:
272 ret = 0; break;
273 case SIG_MDMP:
274 mdmp_dump();
275 break;
278 else
280 printf("Can't get a suitable file signature, aborting\n");
281 ret = 0;
284 if (ret) printf("Done dumping %s\n", name);
285 #ifdef HAVE_MMAP
286 if (munmap(dump_base, dump_total_len) == -1)
287 #endif
289 free( dump_base );
291 close(fd);
293 return ret;
296 void dump_file(const char* name)
298 dump_analysis(name, do_dump, SIG_UNKNOWN);