~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/tools/winedump/dump.c

Version: ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  *      File dumping utility
  3  *
  4  *      Copyright 2001,2007 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 
 48 static void*                    dump_base;
 49 static unsigned long            dump_total_len;
 50 
 51 void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
 52 {
 53     unsigned int i, j;
 54 
 55     printf( "%s%08x: ", prefix, 0 );
 56     if (!ptr)
 57     {
 58         printf("NULL\n");
 59         return;
 60     }
 61     for (i = 0; i < size; i++)
 62     {
 63         printf( "%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ' );
 64         if ((i % 16) == 15)
 65         {
 66             printf( " " );
 67             for (j = 0; j < 16; j++)
 68                 printf( "%c", isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.' );
 69             if (i < size-1) printf( "\n%s%08x: ", prefix, i + 1 );
 70         }
 71     }
 72     if (i % 16)
 73     {
 74         printf( "%*s ", 3 * (16-(i%16)), "" );
 75         for (j = 0; j < i % 16; j++)
 76             printf( "%c", isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.' );
 77     }
 78     printf( "\n" );
 79 }
 80 
 81 static char* dump_want_n(unsigned sz)
 82 {
 83     static char         buffer[4 * 1024];
 84     static unsigned     idx;
 85     char*               ret;
 86 
 87     assert(sz < sizeof(buffer));
 88     if (idx + sz >= sizeof(buffer)) idx = 0;
 89     ret = &buffer[idx];
 90     idx += sz;
 91     return ret;
 92 }
 93 
 94 const char *get_time_str(unsigned long _t)
 95 {
 96     const time_t    t = (const time_t)_t;
 97     const char      *str = ctime(&t);
 98     size_t          len;
 99     char*           buf;
100 
101     if (!str) return "not valid time";
102 
103     len = strlen(str);
104     /* FIXME: I don't get the same values from MS' pedump running under Wine...
105      * I wonder if Wine isn't broken wrt to GMT settings...
106      */
107     if (len && str[len-1] == '\n') len--;
108     buf = dump_want_n(len + 1);
109     if (buf)
110     {
111         memcpy( buf, str, len );
112         buf[len] = 0;
113     }
114     return buf;
115 }
116 
117 unsigned int strlenW( const WCHAR *str )
118 {
119     const WCHAR *s = str;
120     while (*s) s++;
121     return s - str;
122 }
123 
124 void dump_unicode_str( const WCHAR *str, int len )
125 {
126     if (len == -1) len = strlenW( str );
127     printf( "L\"");
128     while (len-- > 0 && *str)
129     {
130         WCHAR c = *str++;
131         switch (c)
132         {
133         case '\n': printf( "\\n" ); break;
134         case '\r': printf( "\\r" ); break;
135         case '\t': printf( "\\t" ); break;
136         case '"':  printf( "\\\"" ); break;
137         case '\\': printf( "\\\\" ); break;
138         default:
139             if (c >= ' ' && c <= 126) putchar(c);
140             else printf( "\\u%04x",c);
141         }
142     }
143     printf( "\"" );
144 }
145 
146 const char* get_symbol_str(const char* symname)
147 {
148     char*       tmp;
149     const char* ret;
150 
151     if (!symname) return "(nil)";
152     if (globals.do_demangle)
153     {
154         parsed_symbol   symbol;
155 
156         symbol_init(&symbol, symname);
157         if (symbol_demangle(&symbol) == -1)
158             ret = symname;
159         else if (symbol.flags & SYM_DATA)
160         {
161             ret = tmp = dump_want_n(strlen(symbol.arg_text[0]) + 1);
162             if (tmp) strcpy(tmp, symbol.arg_text[0]);
163         }
164         else
165         {
166             unsigned int i, len, start = symbol.flags & SYM_THISCALL ? 1 : 0;
167 
168             len = strlen(symbol.return_text) + 3 /* ' __' */ +
169                 strlen(symbol_get_call_convention(&symbol)) + 1 /* ' ' */+
170                 strlen(symbol.function_name) + 1 /* ')' */;
171             if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
172                 len += 4 /* "void" */;
173             else for (i = start; i < symbol.argc; i++)
174                 len += (i > start ? 2 /* ", " */ : 0 /* "" */) + strlen(symbol.arg_text[i]);
175             if (symbol.varargs) len += 5 /* ", ..." */;
176             len += 2; /* ")\0" */
177 
178             ret = tmp = dump_want_n(len);
179             if (tmp)
180             {
181                 sprintf(tmp, "%s __%s %s(",
182                         symbol.return_text,
183                         symbol_get_call_convention(&symbol),
184                         symbol.function_name);
185                 if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
186                     strcat(tmp, "void");
187                 else for (i = start; i < symbol.argc; i++)
188                 {
189                     if (i > start) strcat(tmp, ", ");
190                     strcat(tmp, symbol.arg_text[i]);
191                 }
192                 if (symbol.varargs) strcat(tmp, ", ...");
193                 strcat(tmp, ")");
194             }
195         }
196         symbol_clear(&symbol);
197     }
198     else ret = symname;
199     return ret;
200 }
201 
202 const char* get_guid_str(const GUID* guid)
203 {
204     char* str;
205 
206     str = dump_want_n(39);
207     if (str)
208         sprintf(str, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
209                 guid->Data1, guid->Data2, guid->Data3,
210                 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
211                 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
212     return str;
213 }
214 
215 const void*     PRD(unsigned long prd, unsigned long len)
216 {
217     return (prd + len > dump_total_len) ? NULL : (const char*)dump_base + prd;
218 }
219 
220 unsigned long Offset(const void* ptr)
221 {
222     if (ptr < dump_base) {printf("<<<<<ptr below\n");return 0;}
223     if ((const char *)ptr >= (const char*)dump_base + dump_total_len) {printf("<<<<<ptr above\n");return 0;}
224     return (const char *)ptr - (const char *)dump_base;
225 }
226 
227 static const struct dumper
228 {
229     enum FileSig        kind;
230     enum FileSig        (*get_kind)(void);
231     file_dumper         dumper; /* default dump tool */
232 }
233 dumpers[] =
234 {
235     {SIG_DOS,           get_kind_exec,  dos_dump},
236     {SIG_PE,            get_kind_exec,  pe_dump},
237     {SIG_DBG,           get_kind_dbg,   dbg_dump},
238     {SIG_PDB,           get_kind_pdb,   pdb_dump},
239     {SIG_NE,            get_kind_exec,  ne_dump},
240     {SIG_LE,            get_kind_exec,  le_dump},
241     {SIG_COFFLIB,       get_kind_lib,   lib_dump},
242     {SIG_MDMP,          get_kind_mdmp,  mdmp_dump},
243     {SIG_LNK,           get_kind_lnk,   lnk_dump},
244     {SIG_EMF,           get_kind_emf,   emf_dump},
245     {SIG_UNKNOWN,       NULL,           NULL} /* sentinel */
246 };
247 
248 int dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig)
249 {
250     int                 fd;
251     int                 ret = 1;
252     struct stat         s;
253     const struct dumper *dpr;
254 
255     setbuf(stdout, NULL);
256 
257     fd = open(name, O_RDONLY | O_BINARY);
258     if (fd == -1) fatal("Can't open file");
259 
260     if (fstat(fd, &s) < 0) fatal("Can't get size");
261     dump_total_len = s.st_size;
262 
263 #ifdef HAVE_MMAP
264     if ((dump_base = mmap(NULL, dump_total_len, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
265 #endif
266     {
267         if (!(dump_base = malloc( dump_total_len ))) fatal( "Out of memory" );
268         if ((unsigned long)read( fd, dump_base, dump_total_len ) != dump_total_len) fatal( "Cannot read file" );
269     }
270 
271     printf("Contents of %s: %ld bytes\n\n", name, dump_total_len);
272 
273     for (dpr = dumpers; dpr->kind != SIG_UNKNOWN; dpr++)
274     {
275         if (dpr->get_kind() == dpr->kind &&
276             (wanted_sig == SIG_UNKNOWN || wanted_sig == dpr->kind))
277         {
278             if (fn) fn(); else dpr->dumper();
279             break;
280         }
281     }
282     if (dpr->kind == SIG_UNKNOWN)
283     {
284         printf("Can't get a suitable file signature, aborting\n");
285         ret = 0;
286     }
287 
288     if (ret) printf("Done dumping %s\n", name);
289 #ifdef HAVE_MMAP
290     if (munmap(dump_base, dump_total_len) == -1)
291 #endif
292     {
293         free( dump_base );
294     }
295     close(fd);
296 
297     return ret;
298 }
299 
300 void    dump_file(const char* name)
301 {
302     dump_analysis(name, NULL, SIG_UNKNOWN);
303 }
304 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.