dump.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #ifdef HAVE_UNISTD_H
  26. # include <unistd.h>
  27. #endif
  28. #include <time.h>
  29. #ifdef HAVE_SYS_TYPES_H
  30. # include <sys/types.h>
  31. #endif
  32. #ifdef HAVE_SYS_STAT_H
  33. # include <sys/stat.h>
  34. #endif
  35. #include <fcntl.h>
  36. #include "windef.h"
  37. #include "winbase.h"
  38. #include "winedump.h"
  39. void *dump_base = NULL;
  40. unsigned long dump_total_len = 0;
  41. void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
  42. {
  43. unsigned int i, j;
  44. printf( "%s%08x: ", prefix, 0 );
  45. if (!ptr)
  46. {
  47. printf("NULL\n");
  48. return;
  49. }
  50. for (i = 0; i < size; i++)
  51. {
  52. printf( "%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ' );
  53. if ((i % 16) == 15)
  54. {
  55. printf( " " );
  56. for (j = 0; j < 16; j++)
  57. printf( "%c", isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.' );
  58. if (i < size-1) printf( "\n%s%08x: ", prefix, i + 1 );
  59. }
  60. }
  61. if (i % 16)
  62. {
  63. printf( "%*s ", 3 * (16-(i%16)), "" );
  64. for (j = 0; j < i % 16; j++)
  65. printf( "%c", isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.' );
  66. }
  67. printf( "\n" );
  68. }
  69. static char* dump_want_n(unsigned sz)
  70. {
  71. static char buffer[4 * 1024];
  72. static unsigned idx;
  73. char* ret;
  74. assert(sz < sizeof(buffer));
  75. if (idx + sz >= sizeof(buffer)) idx = 0;
  76. ret = &buffer[idx];
  77. idx += sz;
  78. return ret;
  79. }
  80. const char *get_time_str(unsigned long _t)
  81. {
  82. const time_t t = (const time_t)_t;
  83. const char *str = ctime(&t);
  84. size_t len;
  85. char* buf;
  86. if (!str) return "not valid time";
  87. len = strlen(str);
  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. buf = dump_want_n(len + 1);
  93. if (buf)
  94. {
  95. memcpy( buf, str, len );
  96. buf[len] = 0;
  97. }
  98. return buf;
  99. }
  100. unsigned int strlenW( const WCHAR *str )
  101. {
  102. const WCHAR *s = str;
  103. while (*s) s++;
  104. return s - str;
  105. }
  106. void dump_unicode_str( const WCHAR *str, int len )
  107. {
  108. if (len == -1) len = strlenW( str );
  109. printf( "L\"");
  110. while (len-- > 0 && *str)
  111. {
  112. WCHAR c = *str++;
  113. switch (c)
  114. {
  115. case '\n': printf( "\\n" ); break;
  116. case '\r': printf( "\\r" ); break;
  117. case '\t': printf( "\\t" ); break;
  118. case '"': printf( "\\\"" ); break;
  119. case '\\': printf( "\\\\" ); break;
  120. default:
  121. if (c >= ' ' && c <= 126) putchar(c);
  122. else printf( "\\u%04x",c);
  123. }
  124. }
  125. printf( "\"" );
  126. }
  127. const char* get_symbol_str(const char* symname)
  128. {
  129. char* tmp;
  130. const char* ret;
  131. if (!symname) return "(nil)";
  132. if (globals.do_demangle)
  133. {
  134. parsed_symbol symbol;
  135. symbol_init(&symbol, symname);
  136. if (!symbol_demangle(&symbol))
  137. ret = symname;
  138. else if (symbol.flags & SYM_DATA)
  139. {
  140. ret = tmp = dump_want_n(strlen(symbol.arg_text[0]) + 1);
  141. if (tmp) strcpy(tmp, symbol.arg_text[0]);
  142. }
  143. else
  144. {
  145. unsigned int i, len, start = symbol.flags & SYM_THISCALL ? 1 : 0;
  146. len = strlen(symbol.return_text) + 3 /* ' __' */ +
  147. strlen(symbol_get_call_convention(&symbol)) + 1 /* ' ' */+
  148. strlen(symbol.function_name) + 1 /* ')' */;
  149. if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
  150. len += 4 /* "void" */;
  151. else for (i = start; i < symbol.argc; i++)
  152. len += (i > start ? 2 /* ", " */ : 0 /* "" */) + strlen(symbol.arg_text[i]);
  153. if (symbol.varargs) len += 5 /* ", ..." */;
  154. len += 2; /* ")\0" */
  155. ret = tmp = dump_want_n(len);
  156. if (tmp)
  157. {
  158. sprintf(tmp, "%s __%s %s(",
  159. symbol.return_text,
  160. symbol_get_call_convention(&symbol),
  161. symbol.function_name);
  162. if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
  163. strcat(tmp, "void");
  164. else for (i = start; i < symbol.argc; i++)
  165. {
  166. if (i > start) strcat(tmp, ", ");
  167. strcat(tmp, symbol.arg_text[i]);
  168. }
  169. if (symbol.varargs) strcat(tmp, ", ...");
  170. strcat(tmp, ")");
  171. }
  172. }
  173. symbol_clear(&symbol);
  174. }
  175. else ret = symname;
  176. return ret;
  177. }
  178. const char* get_guid_str(const GUID* guid)
  179. {
  180. char* str;
  181. str = dump_want_n(39);
  182. if (str)
  183. sprintf(str, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
  184. guid->Data1, guid->Data2, guid->Data3,
  185. guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
  186. guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
  187. return str;
  188. }
  189. const char *get_unicode_str( const WCHAR *str, int len )
  190. {
  191. char *buffer;
  192. int i = 0;
  193. if (len == -1) len = strlenW( str );
  194. buffer = dump_want_n( len * 6 + 3);
  195. buffer[i++] = '"';
  196. while (len-- > 0 && *str)
  197. {
  198. WCHAR c = *str++;
  199. switch (c)
  200. {
  201. case '\n': strcpy( buffer + i, "\\n" ); i += 2; break;
  202. case '\r': strcpy( buffer + i, "\\r" ); i += 2; break;
  203. case '\t': strcpy( buffer + i, "\\t" ); i += 2; break;
  204. case '"': strcpy( buffer + i, "\\\"" ); i += 2; break;
  205. case '\\': strcpy( buffer + i, "\\\\" ); i += 2; break;
  206. default:
  207. if (c >= ' ' && c <= 126) buffer[i++] = c;
  208. else i += sprintf( buffer + i, "\\u%04x",c);
  209. }
  210. }
  211. buffer[i++] = '"';
  212. buffer[i] = 0;
  213. return buffer;
  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. unsigned long Offset(const void* ptr)
  220. {
  221. if (ptr < dump_base) {printf("<<<<<ptr below\n");return 0;}
  222. if ((const char *)ptr >= (const char*)dump_base + dump_total_len) {printf("<<<<<ptr above\n");return 0;}
  223. return (const char *)ptr - (const char *)dump_base;
  224. }
  225. static const struct dumper
  226. {
  227. enum FileSig kind;
  228. enum FileSig (*get_kind)(void);
  229. file_dumper dumper; /* default dump tool */
  230. }
  231. dumpers[] =
  232. {
  233. {SIG_DOS, get_kind_exec, dos_dump},
  234. {SIG_PE, get_kind_exec, pe_dump},
  235. {SIG_DBG, get_kind_dbg, dbg_dump},
  236. {SIG_PDB, get_kind_pdb, pdb_dump},
  237. {SIG_NE, get_kind_exec, ne_dump},
  238. {SIG_LE, get_kind_exec, le_dump},
  239. {SIG_COFFLIB, get_kind_lib, lib_dump},
  240. {SIG_MDMP, get_kind_mdmp, mdmp_dump},
  241. {SIG_LNK, get_kind_lnk, lnk_dump},
  242. {SIG_EMF, get_kind_emf, emf_dump},
  243. {SIG_FNT, get_kind_fnt, fnt_dump},
  244. {SIG_TLB, get_kind_tlb, tlb_dump},
  245. {SIG_NLS, get_kind_nls, nls_dump},
  246. {SIG_UNKNOWN, NULL, NULL} /* sentinel */
  247. };
  248. BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig)
  249. {
  250. int fd;
  251. BOOL ret = TRUE;
  252. struct stat s;
  253. const struct dumper *dpr;
  254. setbuf(stdout, NULL);
  255. fd = open(name, O_RDONLY | O_BINARY);
  256. if (fd == -1) fatal("Can't open file");
  257. if (fstat(fd, &s) < 0) fatal("Can't get size");
  258. dump_total_len = s.st_size;
  259. if (!(dump_base = malloc( dump_total_len ))) fatal( "Out of memory" );
  260. if ((unsigned long)read( fd, dump_base, dump_total_len ) != dump_total_len) fatal( "Cannot read file" );
  261. printf("Contents of %s: %ld bytes\n\n", name, dump_total_len);
  262. for (dpr = dumpers; dpr->kind != SIG_UNKNOWN; dpr++)
  263. {
  264. if (dpr->get_kind() == dpr->kind &&
  265. (wanted_sig == SIG_UNKNOWN || wanted_sig == dpr->kind))
  266. {
  267. if (fn) fn(); else dpr->dumper();
  268. break;
  269. }
  270. }
  271. if (dpr->kind == SIG_UNKNOWN)
  272. {
  273. printf("Can't get a suitable file signature, aborting\n");
  274. ret = FALSE;
  275. }
  276. if (ret) printf("Done dumping %s\n", name);
  277. free( dump_base );
  278. close(fd);
  279. return ret;
  280. }
  281. void dump_file(const char* name)
  282. {
  283. dump_analysis(name, NULL, SIG_UNKNOWN);
  284. }