po: Update Turkish translation.
[wine.git] / tools / winedump / dump.c
blob412880c4bd6c9ec180d168c86a8b4f4e8fd744b9
1 /*
2 * File dumping utility
4 * Copyright 2001,2007 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 #include <fcntl.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winedump.h"
43 void *dump_base = NULL;
44 unsigned long dump_total_len = 0;
46 void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
48 unsigned int i, j;
50 printf( "%s%08x: ", prefix, 0 );
51 if (!ptr)
53 printf("NULL\n");
54 return;
56 for (i = 0; i < size; i++)
58 printf( "%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ' );
59 if ((i % 16) == 15)
61 printf( " " );
62 for (j = 0; j < 16; j++)
63 printf( "%c", isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.' );
64 if (i < size-1) printf( "\n%s%08x: ", prefix, i + 1 );
67 if (i % 16)
69 printf( "%*s ", 3 * (16-(i%16)), "" );
70 for (j = 0; j < i % 16; j++)
71 printf( "%c", isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.' );
73 printf( "\n" );
76 static char* dump_want_n(unsigned sz)
78 static char buffer[4 * 1024];
79 static unsigned idx;
80 char* ret;
82 assert(sz < sizeof(buffer));
83 if (idx + sz >= sizeof(buffer)) idx = 0;
84 ret = &buffer[idx];
85 idx += sz;
86 return ret;
89 const char *get_time_str(unsigned long _t)
91 const time_t t = (const time_t)_t;
92 const char *str = ctime(&t);
93 size_t len;
94 char* buf;
96 if (!str) return "not valid time";
98 len = strlen(str);
99 /* FIXME: I don't get the same values from MS' pedump running under Wine...
100 * I wonder if Wine isn't broken wrt to GMT settings...
102 if (len && str[len-1] == '\n') len--;
103 buf = dump_want_n(len + 1);
104 if (buf)
106 memcpy( buf, str, len );
107 buf[len] = 0;
109 return buf;
112 unsigned int strlenW( const WCHAR *str )
114 const WCHAR *s = str;
115 while (*s) s++;
116 return s - str;
119 void dump_unicode_str( const WCHAR *str, int len )
121 if (len == -1) len = strlenW( str );
122 printf( "L\"");
123 while (len-- > 0 && *str)
125 WCHAR c = *str++;
126 switch (c)
128 case '\n': printf( "\\n" ); break;
129 case '\r': printf( "\\r" ); break;
130 case '\t': printf( "\\t" ); break;
131 case '"': printf( "\\\"" ); break;
132 case '\\': printf( "\\\\" ); break;
133 default:
134 if (c >= ' ' && c <= 126) putchar(c);
135 else printf( "\\u%04x",c);
138 printf( "\"" );
141 const char* get_symbol_str(const char* symname)
143 char* tmp;
144 const char* ret;
146 if (!symname) return "(nil)";
147 if (globals.do_demangle)
149 parsed_symbol symbol;
151 symbol_init(&symbol, symname);
152 if (!symbol_demangle(&symbol))
153 ret = symname;
154 else if (symbol.flags & SYM_DATA)
156 ret = tmp = dump_want_n(strlen(symbol.arg_text[0]) + 1);
157 if (tmp) strcpy(tmp, symbol.arg_text[0]);
159 else
161 unsigned int i, len, start = symbol.flags & SYM_THISCALL ? 1 : 0;
163 len = strlen(symbol.return_text) + 3 /* ' __' */ +
164 strlen(symbol_get_call_convention(&symbol)) + 1 /* ' ' */+
165 strlen(symbol.function_name) + 1 /* ')' */;
166 if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
167 len += 4 /* "void" */;
168 else for (i = start; i < symbol.argc; i++)
169 len += (i > start ? 2 /* ", " */ : 0 /* "" */) + strlen(symbol.arg_text[i]);
170 if (symbol.varargs) len += 5 /* ", ..." */;
171 len += 2; /* ")\0" */
173 ret = tmp = dump_want_n(len);
174 if (tmp)
176 sprintf(tmp, "%s __%s %s(",
177 symbol.return_text,
178 symbol_get_call_convention(&symbol),
179 symbol.function_name);
180 if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
181 strcat(tmp, "void");
182 else for (i = start; i < symbol.argc; i++)
184 if (i > start) strcat(tmp, ", ");
185 strcat(tmp, symbol.arg_text[i]);
187 if (symbol.varargs) strcat(tmp, ", ...");
188 strcat(tmp, ")");
191 symbol_clear(&symbol);
193 else ret = symname;
194 return ret;
197 const char* get_guid_str(const GUID* guid)
199 char* str;
201 str = dump_want_n(39);
202 if (str)
203 sprintf(str, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
204 guid->Data1, guid->Data2, guid->Data3,
205 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
206 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
207 return str;
210 const char *get_unicode_str( const WCHAR *str, int len )
212 char *buffer;
213 int i = 0;
215 if (len == -1) len = strlenW( str );
216 buffer = dump_want_n( len * 6 + 3);
217 buffer[i++] = '"';
218 while (len-- > 0 && *str)
220 WCHAR c = *str++;
221 switch (c)
223 case '\n': strcpy( buffer + i, "\\n" ); i += 2; break;
224 case '\r': strcpy( buffer + i, "\\r" ); i += 2; break;
225 case '\t': strcpy( buffer + i, "\\t" ); i += 2; break;
226 case '"': strcpy( buffer + i, "\\\"" ); i += 2; break;
227 case '\\': strcpy( buffer + i, "\\\\" ); i += 2; break;
228 default:
229 if (c >= ' ' && c <= 126) buffer[i++] = c;
230 else i += sprintf( buffer + i, "\\u%04x",c);
233 buffer[i++] = '"';
234 buffer[i] = 0;
235 return buffer;
238 const void* PRD(unsigned long prd, unsigned long len)
240 return (prd + len > dump_total_len) ? NULL : (const char*)dump_base + prd;
243 unsigned long Offset(const void* ptr)
245 if (ptr < dump_base) {printf("<<<<<ptr below\n");return 0;}
246 if ((const char *)ptr >= (const char*)dump_base + dump_total_len) {printf("<<<<<ptr above\n");return 0;}
247 return (const char *)ptr - (const char *)dump_base;
250 static const struct dumper
252 enum FileSig kind;
253 enum FileSig (*get_kind)(void);
254 file_dumper dumper; /* default dump tool */
256 dumpers[] =
258 {SIG_DOS, get_kind_exec, dos_dump},
259 {SIG_PE, get_kind_exec, pe_dump},
260 {SIG_DBG, get_kind_dbg, dbg_dump},
261 {SIG_PDB, get_kind_pdb, pdb_dump},
262 {SIG_NE, get_kind_exec, ne_dump},
263 {SIG_LE, get_kind_exec, le_dump},
264 {SIG_COFFLIB, get_kind_lib, lib_dump},
265 {SIG_MDMP, get_kind_mdmp, mdmp_dump},
266 {SIG_LNK, get_kind_lnk, lnk_dump},
267 {SIG_EMF, get_kind_emf, emf_dump},
268 {SIG_FNT, get_kind_fnt, fnt_dump},
269 {SIG_TLB, get_kind_tlb, tlb_dump},
270 {SIG_NLS, get_kind_nls, nls_dump},
271 {SIG_UNKNOWN, NULL, NULL} /* sentinel */
274 BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig)
276 int fd;
277 BOOL ret = TRUE;
278 struct stat s;
279 const struct dumper *dpr;
281 setbuf(stdout, NULL);
283 fd = open(name, O_RDONLY | O_BINARY);
284 if (fd == -1) fatal("Can't open file");
286 if (fstat(fd, &s) < 0) fatal("Can't get size");
287 dump_total_len = s.st_size;
289 if (!(dump_base = malloc( dump_total_len ))) fatal( "Out of memory" );
290 if ((unsigned long)read( fd, dump_base, dump_total_len ) != dump_total_len) fatal( "Cannot read file" );
292 printf("Contents of %s: %ld bytes\n\n", name, dump_total_len);
294 for (dpr = dumpers; dpr->kind != SIG_UNKNOWN; dpr++)
296 if (dpr->get_kind() == dpr->kind &&
297 (wanted_sig == SIG_UNKNOWN || wanted_sig == dpr->kind))
299 if (fn) fn(); else dpr->dumper();
300 break;
303 if (dpr->kind == SIG_UNKNOWN)
305 printf("Can't get a suitable file signature, aborting\n");
306 ret = FALSE;
309 if (ret) printf("Done dumping %s\n", name);
310 free( dump_base );
311 close(fd);
313 return ret;
316 void dump_file(const char* name)
318 dump_analysis(name, NULL, SIG_UNKNOWN);