1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5#include "formattype.h"
6
7#define MAX_INTERFACES_IMPLEMENTED 256 // unused
8#define MAX_CLASSNAME_LENGTH 1024 // single global buffer size
9#define MAX_MEMBER_LENGTH 1024 // single global buffer size
10#define MAX_SIGNATURE_LENGTH 2048 // single global buffer size
11#define DESCR_SIZE 8 // unused
12
13#define MAX_FILENAME_LENGTH 2048 //256
14
15#define MODE_DUMP_ALL 0
16#define MODE_DUMP_CLASS 1
17#define MODE_DUMP_CLASS_METHOD 2
18#define MODE_DUMP_CLASS_METHOD_SIG 3
19#define MODE_GUI 4
20
21BOOL Disassemble(IMDInternalImport *pImport, BYTE *pCode, void *GUICookie, mdToken FuncToken, ParamDescriptor* pszArgname, ULONG ulArgs);
22BOOL Decompile(IMDInternalImport *pImport, BYTE *pCode);
23OPCODE DecodeOpcode(const BYTE *pCode, DWORD *pdwLen);
24struct LineCodeDescr
25{
26 ULONG Line;
27 ULONG Column;
28 ULONG LineEnd;
29 ULONG ColumnEnd;
30 ULONG PC;
31 ULONG_PTR FileToken;
32};
33
34void printLine(void* GUICookie, __in __nullterminated const char* string);
35void printLineW(void* GUICookie, __in __nullterminated const WCHAR* string);
36void printError(void* GUICookie, __in __nullterminated const char* string);
37
38char* AnsiToUtf(__in __nullterminated const char* sz);
39char* UnicodeToAnsi(__in __nullterminated const WCHAR* wz);
40char* UnicodeToUtf(__in __nullterminated const WCHAR* wz);
41WCHAR* UtfToUnicode(__in __nullterminated const char* sz);
42WCHAR* AnsiToUnicode(__in __nullterminated const char* sz);
43void GetInputFileFullPath();
44
45char* RstrUTF(unsigned id);
46WCHAR* RstrW(unsigned id);
47char* RstrANSI(unsigned id);
48
49
50BOOL DumpMethod(mdToken FuncToken, const char *pszClassName, DWORD dwEntryPointToken,void *GUICookie,BOOL DumpBody);
51BOOL DumpField(mdToken FuncToken, const char *pszClassName,void *GUICookie, BOOL DumpBody);
52BOOL DumpEvent(mdToken FuncToken, const char *pszClassName, DWORD dwClassAttrs, void *GUICookie, BOOL DumpBody);
53BOOL DumpProp(mdToken FuncToken, const char *pszClassName, DWORD dwClassAttrs, void *GUICookie, BOOL DumpBody);
54void dumpEHInfo(IMDInternalImport *pImport, void *GUICookie);
55BOOL DumpClass(mdTypeDef cl, DWORD dwEntryPointToken, void* GUICookie, ULONG WhatToDump);
56// WhatToDump: 0-title only; 1-pack,size and custom attrs; 2-everything
57BOOL GetClassLayout(mdTypeDef cl, ULONG* pulPackSize, ULONG* pulClassSize);
58void DumpCustomAttribute(mdCustomAttribute tkCA, void *GUICookie, bool bWithOwner);
59void DumpCustomAttributes(mdToken tkOwner, void *GUICookie);
60void DumpByteArray(__inout __nullterminated char* szString, const BYTE* pBlob, ULONG ulLen, void* GUICookie);
61char* DumpDataPtr(__inout __nullterminated char* buffer, DWORD ptr, DWORD size);
62
63void PrettyPrintToken(__inout __nullterminated char* szString, mdToken tk, IMDInternalImport *pImport,void* GUICookie,mdToken FuncToken); //TypeDef,TypeRef,TypeSpec,MethodDef,FieldDef,MemberRef,MethodSpec,String
64void DumpPermissions(mdToken tkOwner, void* GUICookie);
65void DumpHeader(IMAGE_COR20_HEADER *CORHeader, void* GUICookie);
66void DumpHeaderDetails(IMAGE_COR20_HEADER *CORHeader, void* GUICookie);
67void DumpMetaInfo(__in __nullterminated const WCHAR* pszFileName, __in_opt __nullterminated const char* pszObjFileName, void* GUICookie);
68void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie);
69BOOL DumpFile();
70void Cleanup();
71void CreateProgressBar(LONG lRange);
72BOOL ProgressStep();
73void DestroyProgressBar();
74char * DumpQString(void* GUICookie,
75 __in __nullterminated const char* szToDump,
76 __in __nullterminated const char* szPrefix,
77 unsigned uMaxLen);
78void DumpVtable(void* GUICookie);
79char* DumpUnicodeString(void* GUICookie,
80 __inout __nullterminated char* szString,
81 __in_ecount(cbString) WCHAR* pszString,
82 ULONG cbString,
83 bool SwapString = false);
84
85void TokenSigInit(IMDInternalImport *pImport);
86void TokenSigDelete();
87bool IsSpecialNumber(const char*);
88
89
90//---------------- see DMAN.CPP--------------------------------------------------
91struct LocalComTypeDescr
92{
93 mdExportedType tkComTypeTok;
94 mdTypeDef tkTypeDef;
95 mdToken tkImplementation;
96 WCHAR* wzName;
97 DWORD dwFlags;
98 LocalComTypeDescr() { wzName=NULL; };
99 ~LocalComTypeDescr() { if(wzName) SDELETE(wzName); };
100};
101
102struct MTokName
103{
104 mdFile tok;
105 WCHAR* name;
106 MTokName() { tok = 0; name = NULL; };
107 ~MTokName() { if(name) SDELETE(name); };
108};
109extern BOOL g_fPrettyPrint;
110extern MTokName* rExeloc;
111extern ULONG nExelocs;
112void DumpImplementation(mdToken tkImplementation,
113 DWORD dwOffset,
114 __inout __nullterminated char* szString,
115 void* GUICookie);
116void DumpComType(LocalComTypeDescr* pCTD,
117 __inout __nullterminated char* szString,
118 void* GUICookie);
119void DumpManifest(void* GUICookie);
120void DumpTypedefs(void* GUICookie);
121IMetaDataAssemblyImport* GetAssemblyImport(void* GUICookie);
122
123void DumpRTFPrefix(void* GUICookie, BOOL fFontDefault);
124void DumpRTFPostfix(void* GUICookie);
125
126//-------------------------------------------------------------------------------
127#define NEW_TRY_BLOCK 0x80000000
128#define PUT_INTO_CODE 0x40000000
129#define ERR_OUT_OF_CODE 0x20000000
130#define SEH_NEW_PUT_MASK (NEW_TRY_BLOCK | PUT_INTO_CODE | ERR_OUT_OF_CODE)
131//-------------------------------------------------------------------------------
132
133// As much as 2 * SZSTRING_SIZE seems to be needed for some corner cases. ILDasm is
134// unfortunately full of constants and artificial limits, and may produce invalid
135// output or overrun a buffer when fed with something unusual (like a type with
136// thousands of generic parameters). Fixing all such problems effectively means
137// rewriting the tool from scratch. Until this happens, let's at least try to keep it
138// working for reasonable input.
139#define UNIBUF_SIZE 262144
140extern WCHAR wzUniBuf[]; // defined in dis.cpp
141
142#define SZSTRING_SIZE 131072
143extern char szString[]; // defined in dis.cpp
144
145char *DumpGenericPars(__inout_ecount(SZSTRING_SIZE) char* szString,
146 mdToken tok,
147 void* GUICookie=NULL,
148 BOOL fSplit=FALSE);
149
150#include "safemath.h"
151#define SZSTRING_SIZE_M4 (SZSTRING_SIZE - 4)
152#define CHECK_REMAINING_SIZE if(ovadd_le((size_t)szString, SZSTRING_SIZE_M4, (size_t)szptr)) break;
153#define SZSTRING_REMAINING_SIZE(x) (ovadd_le((size_t)szString,SZSTRING_SIZE,(size_t)(x))?0:(SZSTRING_SIZE-((size_t)(x)-(size_t)szString)))
154
155typedef int (STDAPICALLTYPE *MetaDataGetDispenserFunc) (
156 REFCLSID rclsid, // The class to desired.
157 REFIID riid, // Interface wanted on class factory.
158 LPVOID FAR *ppv); // Return interface pointer here.
159
160typedef int (STDAPICALLTYPE *GetMetaDataInternalInterfaceFunc) (
161 LPVOID pData, // [IN] in memory metadata section
162 ULONG cbData, // [IN] size of the metadata section
163 DWORD flags, // [IN] CorOpenFlags
164 REFIID riid, // [IN] desired interface
165 void **ppv); // [OUT] returned interface
166
167typedef int (STDAPICALLTYPE *GetMetaDataInternalInterfaceFromPublicFunc) (
168 IUnknown *pv, // [IN] Given interface
169 REFIID riid, // [IN] desired interface
170 void **ppv); // [OUT] returned interface
171
172typedef int (STDAPICALLTYPE *GetMetaDataPublicInterfaceFromInternalFunc) (
173 void *pv, // [IN] Given interface
174 REFIID riid, // [IN] desired interface
175 void **ppv); // [OUT] returned interface
176
177extern MetaDataGetDispenserFunc metaDataGetDispenser;
178extern GetMetaDataInternalInterfaceFunc getMetaDataInternalInterface;
179extern GetMetaDataInternalInterfaceFromPublicFunc getMetaDataInternalInterfaceFromPublic;
180extern GetMetaDataPublicInterfaceFromInternalFunc getMetaDataPublicInterfaceFromInternal;
181
182