2
0

MDFeatureElems.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * PERWAPI - An API for Reading and Writing PE Files
  3. *
  4. * Copyright (c) Diane Corney, Queensland University of Technology, 2004.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the PERWAPI Copyright as included with this
  8. * distribution in the file PERWAPIcopyright.rtf.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY as is explained in the copyright notice.
  12. *
  13. * The author may be contacted at d.corney@qut.edu.au
  14. *
  15. * Version Date: 26/01/07
  16. */
  17. using System;
  18. using System.IO;
  19. using System.Collections;
  20. using System.Security.Cryptography;
  21. namespace QUT.PERWAPI
  22. {
  23. /**************************************************************************/
  24. /// <summary>
  25. /// Base class for Event and Property descriptors
  26. /// </summary>
  27. public abstract class Feature : MetaDataElement
  28. {
  29. private static readonly int INITSIZE = 5;
  30. private static readonly ushort specialName = 0x200;
  31. private static readonly ushort rtsSpecialName = 0x400;
  32. private static readonly ushort noSpecialName = 0xFDFF;
  33. private static readonly ushort noRTSSpecialName = 0xFBFF;
  34. protected ClassDef parent;
  35. protected ushort flags = 0;
  36. protected string name;
  37. protected int tide = 0;
  38. protected uint nameIx;
  39. protected MethodSemantics[] methods = new MethodSemantics[INITSIZE];
  40. /*-------------------- Constructors ---------------------------------*/
  41. internal Feature(string name, ClassDef par)
  42. {
  43. parent = par;
  44. this.name = name;
  45. }
  46. internal Feature() { }
  47. internal static string[] GetFeatureNames(PEReader buff, MDTable tabIx, MDTable mapTabIx,
  48. ClassDef theClass, uint classIx)
  49. {
  50. buff.SetElementPosition(mapTabIx, 0);
  51. uint start = 0, end = 0, i = 0;
  52. for (; (i < buff.GetTableSize(tabIx)) && (start == 0); i++)
  53. {
  54. if (buff.GetIndex(MDTable.TypeDef) == classIx)
  55. {
  56. start = buff.GetIndex(tabIx);
  57. }
  58. }
  59. if (start == 0) return null;
  60. if (i < buff.GetTableSize(mapTabIx))
  61. {
  62. uint junk = buff.GetIndex(MDTable.TypeDef);
  63. end = buff.GetIndex(tabIx);
  64. }
  65. else
  66. end = buff.GetTableSize(tabIx);
  67. if (tabIx == MDTable.Event)
  68. theClass.eventIx = start;
  69. else
  70. theClass.propIx = start;
  71. string[] names = new string[end - start];
  72. buff.SetElementPosition(tabIx, start);
  73. for (i = start; i < end; i++)
  74. {
  75. uint junk = buff.ReadUInt16();
  76. names[i] = buff.GetString();
  77. if (tabIx == MDTable.Event)
  78. junk = buff.GetCodedIndex(CIx.TypeDefOrRef);
  79. else
  80. junk = buff.GetBlobIx();
  81. }
  82. return names;
  83. }
  84. /*------------------------- public set and get methods --------------------------*/
  85. /// <summary>
  86. /// Set the specialName attribute for this Event or Property
  87. /// </summary>
  88. public void SetSpecialName() { flags |= specialName; }
  89. public bool HasSpecialName() { return (flags & specialName) != 0; }
  90. public void ClearSpecialName() { flags &= noSpecialName; }
  91. /// <summary>
  92. /// Set the RTSpecialName attribute for this Event or Property
  93. /// </summary>
  94. public void SetRTSpecialName() { flags |= rtsSpecialName; }
  95. public bool HasRTSSpecialName() { return (flags & rtsSpecialName) != 0; }
  96. public void ClearRTSSpecialName() { flags &= noRTSSpecialName; }
  97. public string Name() { return name; }
  98. public void SetName(string nam) { name = nam; }
  99. internal void AddMethod(MethodSemantics meth)
  100. {
  101. if (tide == methods.Length)
  102. {
  103. MethodSemantics[] mTmp = methods;
  104. methods = new MethodSemantics[tide * 2];
  105. for (int i = 0; i < tide; i++)
  106. {
  107. methods[i] = mTmp[i];
  108. }
  109. }
  110. methods[tide++] = meth;
  111. }
  112. public void AddMethod(MethodDef meth, MethodType mType)
  113. {
  114. AddMethod(new MethodSemantics(mType, meth, this));
  115. }
  116. public MethodDef GetMethod(MethodType mType)
  117. {
  118. for (int i = 0; i < tide; i++)
  119. {
  120. if (methods[i].GetMethodType() == mType)
  121. return methods[i].GetMethod();
  122. }
  123. return null;
  124. }
  125. public void RemoveMethod(MethodDef meth)
  126. {
  127. bool found = false;
  128. for (int i = 0; i < tide; i++)
  129. {
  130. if (found)
  131. methods[i - 1] = methods[i];
  132. else if (methods[i].GetMethod() == meth)
  133. found = true;
  134. }
  135. }
  136. public void RemoveMethod(MethodType mType)
  137. {
  138. bool found = false;
  139. for (int i = 0; i < tide; i++)
  140. {
  141. if (found)
  142. methods[i - 1] = methods[i];
  143. else if (methods[i].GetMethodType() == mType)
  144. found = true;
  145. }
  146. }
  147. internal void SetParent(ClassDef paren)
  148. {
  149. parent = paren;
  150. }
  151. internal ClassDef GetParent()
  152. {
  153. return parent;
  154. }
  155. }
  156. /*****************************************************************************/
  157. /// <summary>
  158. /// Descriptor for an event
  159. /// </summary>
  160. public class Event : Feature
  161. {
  162. Type eventType;
  163. uint typeIx = 0;
  164. /*-------------------- Constructors ---------------------------------*/
  165. internal Event(string name, Type eType, ClassDef parent)
  166. : base(name, parent)
  167. {
  168. eventType = eType;
  169. tabIx = MDTable.Event;
  170. }
  171. internal Event(PEReader buff)
  172. {
  173. flags = buff.ReadUInt16();
  174. name = buff.GetString();
  175. typeIx = buff.GetCodedIndex(CIx.TypeDefOrRef);
  176. tabIx = MDTable.Event;
  177. }
  178. internal static void Read(PEReader buff, TableRow[] events)
  179. {
  180. for (int i = 0; i < events.Length; i++)
  181. events[i] = new Event(buff);
  182. }
  183. internal static string[] ReadNames(PEReader buff, ClassDef theClass, uint classIx)
  184. {
  185. return Feature.GetFeatureNames(buff, MDTable.Event, MDTable.EventMap, theClass, classIx);
  186. }
  187. internal override void Resolve(PEReader buff)
  188. {
  189. eventType = (Type)buff.GetCodedElement(CIx.TypeDefOrRef, typeIx);
  190. }
  191. /*------------------------- public set and get methods --------------------------*/
  192. public Type GetEventType() { return eventType; }
  193. /*----------------------------- internal functions ------------------------------*/
  194. internal void ChangeRefsToDefs(ClassDef newType, ClassDef[] oldTypes)
  195. {
  196. throw new NotYetImplementedException("Merge for Events");
  197. }
  198. internal sealed override void BuildTables(MetaDataOut md)
  199. {
  200. md.AddToTable(MDTable.Event, this);
  201. nameIx = md.AddToStringsHeap(name);
  202. eventType.BuildMDTables(md);
  203. for (int i = 0; i < tide; i++)
  204. {
  205. methods[i].BuildMDTables(md);
  206. }
  207. }
  208. internal override void BuildCILInfo(CILWriter output)
  209. {
  210. eventType.BuildCILInfo(output);
  211. }
  212. internal static uint Size(MetaData md)
  213. {
  214. return 2 + md.StringsIndexSize() + md.CodedIndexSize(CIx.TypeDefOrRef);
  215. }
  216. internal sealed override void Write(PEWriter output)
  217. {
  218. output.Write(flags);
  219. output.StringsIndex(nameIx);
  220. output.WriteCodedIndex(CIx.TypeDefOrRef, eventType);
  221. }
  222. internal override void Write(CILWriter output)
  223. {
  224. throw new NotYetImplementedException("Write CIL for event");
  225. }
  226. internal sealed override uint GetCodedIx(CIx code)
  227. {
  228. switch (code)
  229. {
  230. case (CIx.HasCustomAttr): return 10;
  231. case (CIx.HasSemantics): return 0;
  232. }
  233. return 0;
  234. }
  235. }
  236. /**************************************************************************/
  237. /// <summary>
  238. /// Descriptor for the Property of a class
  239. /// </summary>
  240. public class Property : Feature
  241. {
  242. internal static readonly byte PropertyTag = 0x8;
  243. Constant constVal;
  244. uint typeBlobIx = 0;
  245. Type[] parList;
  246. Type returnType;
  247. uint numPars = 0;
  248. /*-------------------- Constructors ---------------------------------*/
  249. internal Property(string name, Type retType, Type[] pars, ClassDef parent)
  250. : base(name, parent)
  251. {
  252. returnType = retType;
  253. parList = pars;
  254. if (pars != null) numPars = (uint)pars.Length;
  255. tabIx = MDTable.Property;
  256. }
  257. internal Property(PEReader buff)
  258. {
  259. flags = buff.ReadUInt16();
  260. name = buff.GetString();
  261. typeBlobIx = buff.GetBlobIx();
  262. tabIx = MDTable.Property;
  263. }
  264. internal static void Read(PEReader buff, TableRow[] props)
  265. {
  266. for (int i = 0; i < props.Length; i++)
  267. props[i] = new Property(buff);
  268. }
  269. internal static string[] ReadNames(PEReader buff, ClassDef theClass, uint classIx)
  270. {
  271. return Feature.GetFeatureNames(buff, MDTable.Property, MDTable.PropertyMap, theClass, classIx);
  272. }
  273. internal sealed override void Resolve(PEReader buff)
  274. {
  275. buff.ReadPropertySig(typeBlobIx, this);
  276. }
  277. /// <summary>
  278. /// Add an initial value for this property
  279. /// </summary>
  280. /// <param name="constVal">the initial value for this property</param>
  281. public void AddInitValue(Constant constVal)
  282. {
  283. this.constVal = constVal;
  284. }
  285. public Constant GetInitValue() { return constVal; }
  286. public void RemoveInitValue() { constVal = null; }
  287. public Type GetPropertyType() { return returnType; }
  288. public void SetPropertyType(Type pType) { returnType = pType; }
  289. public Type[] GetPropertyParams() { return parList; }
  290. public void SetPropertyParams(Type[] parTypes)
  291. {
  292. parList = parTypes;
  293. if (parList != null) numPars = (uint)parList.Length;
  294. }
  295. internal void ChangeRefsToDefs(ClassDef newType, ClassDef[] oldTypes)
  296. {
  297. throw new NotYetImplementedException("Merge for Properties");
  298. }
  299. internal sealed override void BuildTables(MetaDataOut md)
  300. {
  301. md.AddToTable(MDTable.Property, this);
  302. nameIx = md.AddToStringsHeap(name);
  303. for (int i = 0; i < numPars; i++)
  304. parList[i].BuildMDTables(md);
  305. for (int i = 0; i < tide; i++)
  306. methods[i].BuildMDTables(md);
  307. if (constVal != null)
  308. {
  309. ConstantElem constElem = new ConstantElem(this, constVal);
  310. constElem.BuildMDTables(md);
  311. }
  312. }
  313. internal sealed override void BuildSignatures(MetaDataOut md)
  314. {
  315. MemoryStream sig = new MemoryStream();
  316. sig.WriteByte(PropertyTag);
  317. MetaDataOut.CompressNum(BlobUtil.CompressUInt(numPars), sig);
  318. returnType.TypeSig(sig);
  319. for (int i = 0; i < numPars; i++)
  320. {
  321. parList[i].BuildSignatures(md);
  322. parList[i].TypeSig(sig);
  323. }
  324. typeBlobIx = md.AddToBlobHeap(sig.ToArray());
  325. done = false;
  326. }
  327. internal override void BuildCILInfo(CILWriter output)
  328. {
  329. returnType.BuildCILInfo(output);
  330. for (int i = 0; i < numPars; i++)
  331. {
  332. parList[i].BuildCILInfo(output);
  333. }
  334. }
  335. internal static uint Size(MetaData md)
  336. {
  337. return 2 + md.StringsIndexSize() + md.BlobIndexSize();
  338. }
  339. internal sealed override void Write(PEWriter output)
  340. {
  341. output.Write(flags);
  342. output.StringsIndex(nameIx);
  343. output.BlobIndex(typeBlobIx);
  344. }
  345. internal override void Write(CILWriter output)
  346. {
  347. throw new NotYetImplementedException("Write CIL for property");
  348. }
  349. internal sealed override uint GetCodedIx(CIx code)
  350. {
  351. switch (code)
  352. {
  353. case (CIx.HasCustomAttr): return 9;
  354. case (CIx.HasConstant): return 2;
  355. case (CIx.HasSemantics): return 1;
  356. }
  357. return 0;
  358. }
  359. }
  360. }