MDElements.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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. /// Abstract class to represent a row of the Meta Data Tables
  26. /// </summary>
  27. public abstract class TableRow
  28. {
  29. internal PEReader buffer;
  30. private uint row = 0;
  31. /// <summary>
  32. /// The index of the Meta Data Table containing this element
  33. /// </summary>
  34. protected MDTable tabIx;
  35. /*-------------------- Constructors ---------------------------------*/
  36. internal TableRow() { }
  37. internal TableRow(PEReader buff, uint ix, MDTable tableIx)
  38. {
  39. buffer = buff;
  40. row = ix;
  41. tabIx = tableIx;
  42. }
  43. /// <summary>
  44. /// The row number of this element in the Meta Data Table
  45. /// </summary>
  46. public uint Row
  47. {
  48. get { return row; }
  49. set { row = value; }
  50. }
  51. }
  52. /****************************************************/
  53. /// <summary>
  54. /// Base class for all Meta Data table elements
  55. /// </summary>
  56. public abstract class MetaDataElement : TableRow, IComparable
  57. {
  58. /// <summary>
  59. /// The list of custom attributes associated with this meta data element
  60. /// </summary>
  61. protected ArrayList customAttributes;
  62. protected bool done = false;
  63. protected bool sortTable = false;
  64. internal bool unresolved = false;
  65. /*-------------------- Constructors ---------------------------------*/
  66. internal MetaDataElement() { }
  67. /// <summary>
  68. /// Get any custom attributes associated with this meta data element
  69. /// </summary>
  70. /// <returns>Array of custom attribute descriptors</returns>
  71. public CustomAttribute[] GetCustomAttributes()
  72. {
  73. if (customAttributes == null) return new CustomAttribute[0];
  74. return (CustomAttribute[])customAttributes.ToArray(typeof(CustomAttribute));
  75. }
  76. /// <summary>
  77. /// Associate some custom attribute(s) with this meta data element
  78. /// </summary>
  79. /// <param name="cas">list of custom attributes</param>
  80. public void SetCustomAttributes(CustomAttribute[] cas)
  81. {
  82. if (cas == null)
  83. customAttributes = null;
  84. else
  85. customAttributes = new ArrayList(cas);
  86. }
  87. // FIXME: this is temporary
  88. public string GetNameString()
  89. {
  90. return this.NameString();
  91. }
  92. internal virtual bool isDef() { return false; }
  93. internal virtual void Resolve(PEReader buff) { }
  94. internal virtual void ResolveDetails(PEReader buff) { }
  95. internal virtual uint GetCodedIx(CIx code) { return 0; }
  96. internal bool NeedToSort() { return sortTable; }
  97. internal virtual uint SortKey()
  98. {
  99. throw new PEFileException("Trying to sort table of " + this);
  100. //return 0;
  101. }
  102. /// <summary>
  103. /// Add a custom attribute to this item
  104. /// </summary>
  105. /// <param name="ctorMeth">the constructor method for this attribute</param>
  106. /// <param name="val">the byte value of the parameters</param>
  107. public void AddCustomAttribute(Method ctorMeth, byte[] val)
  108. {
  109. if (customAttributes == null)
  110. {
  111. customAttributes = new ArrayList();
  112. }
  113. customAttributes.Add(new CustomAttribute(this, ctorMeth, val));
  114. }
  115. /// <summary>
  116. /// Add a custom attribute to this item
  117. /// </summary>
  118. /// <param name="ctorMeth">the constructor method for this attribute</param>
  119. /// <param name="cVals">the constant values of the parameters</param>
  120. public void AddCustomAttribute(Method ctorMeth, Constant[] cVals)
  121. {
  122. if (customAttributes == null)
  123. {
  124. customAttributes = new ArrayList();
  125. }
  126. customAttributes.Add(new CustomAttribute(this, ctorMeth, cVals));
  127. }
  128. /// <summary>
  129. /// Associate a custom attribute with this meta data element
  130. /// </summary>
  131. public void AddCustomAttribute(CustomAttribute ca)
  132. {
  133. if (customAttributes == null)
  134. {
  135. customAttributes = new ArrayList();
  136. }
  137. customAttributes.Add(ca);
  138. }
  139. internal uint Token()
  140. {
  141. if (Row == 0) throw new Exception("Meta data token is zero!!");
  142. return (((uint)tabIx << 24) | Row);
  143. }
  144. internal void BuildMDTables(MetaDataOut md)
  145. {
  146. if (done) return;
  147. done = true;
  148. if (Diag.DiagOn) Console.WriteLine("In BuildMDTables");
  149. BuildTables(md);
  150. if (customAttributes != null)
  151. {
  152. for (int i = 0; i < customAttributes.Count; i++)
  153. {
  154. CustomAttribute ca = (CustomAttribute)customAttributes[i];
  155. ca.BuildTables(md);
  156. }
  157. }
  158. }
  159. internal virtual void BuildTables(MetaDataOut md) { }
  160. internal virtual void BuildSignatures(MetaDataOut md)
  161. {
  162. done = false;
  163. }
  164. internal virtual void BuildCILInfo(CILWriter output) { }
  165. internal virtual void AddToTable(MetaDataOut md)
  166. {
  167. md.AddToTable(tabIx, this);
  168. }
  169. internal virtual void Write(PEWriter output) { }
  170. internal virtual void Write(CILWriter output)
  171. {
  172. throw new Exception("CIL backend not yet fully implemented - " + GetType().ToString());
  173. }
  174. internal virtual string NameString() { return "NoName"; }
  175. internal void DescriptorError(MetaDataElement elem)
  176. {
  177. throw new DescriptorException(elem.NameString());
  178. }
  179. #region IComparable Members
  180. public int CompareTo(object obj)
  181. {
  182. uint otherKey = ((MetaDataElement)obj).SortKey();
  183. uint thisKey = SortKey();
  184. if (thisKey == otherKey)
  185. {
  186. if (this is GenericParam)
  187. {
  188. if (((GenericParam)this).Index < ((GenericParam)obj).Index)
  189. return -1;
  190. else
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. if (thisKey < otherKey) return -1;
  196. return 1;
  197. }
  198. #endregion
  199. }
  200. /**************************************************************************/
  201. /// <summary>
  202. /// Descriptor for GenericParamConstraint
  203. /// </summary>
  204. public class GenericParamConstraint : MetaDataElement
  205. {
  206. uint parentIx, constraintIx;
  207. GenericParam parent;
  208. Type constraint;
  209. // Class constraint;
  210. /*-------------------- Constructors ---------------------------------*/
  211. public GenericParamConstraint(GenericParam parent, Type constraint)
  212. //public GenericParamConstraint(GenericParam parent, Class constraint)
  213. {
  214. this.parent = parent;
  215. this.constraint = constraint;
  216. tabIx = MDTable.GenericParamConstraint;
  217. }
  218. internal GenericParamConstraint(PEReader buff)
  219. {
  220. parentIx = buff.GetIndex(MDTable.GenericParam);
  221. constraintIx = buff.GetCodedIndex(CIx.TypeDefOrRef);
  222. tabIx = MDTable.GenericParamConstraint;
  223. }
  224. internal static void Read(PEReader buff, TableRow[] gpars)
  225. {
  226. for (int i = 0; i < gpars.Length; i++)
  227. gpars[i] = new GenericParamConstraint(buff);
  228. }
  229. internal override void Resolve(PEReader buff)
  230. {
  231. parent = (GenericParam)buff.GetElement(MDTable.GenericParam, parentIx);
  232. parent.AddConstraint((Type)buff.GetCodedElement(CIx.TypeDefOrRef, constraintIx));
  233. }
  234. internal static uint Size(MetaData md)
  235. {
  236. return md.TableIndexSize(MDTable.GenericParam) + md.CodedIndexSize(CIx.TypeDefOrRef);
  237. }
  238. internal override void Write(PEWriter output)
  239. {
  240. output.WriteIndex(MDTable.GenericParam, parent.Row);
  241. output.WriteCodedIndex(CIx.TypeDefOrRef, constraint);
  242. }
  243. }
  244. /**************************************************************************/
  245. /// <summary>
  246. /// Descriptor for pinvoke information for a method
  247. /// </summary>
  248. public class ImplMap : MetaDataElement
  249. {
  250. private static readonly ushort NoMangle = 0x01;
  251. ushort flags;
  252. MethodDef meth;
  253. string importName;
  254. uint iNameIx, scopeIx = 0, memForIndex = 0;
  255. ModuleRef importScope;
  256. /*-------------------- Constructors ---------------------------------*/
  257. internal ImplMap(ushort flag, MethodDef implMeth, string iName, ModuleRef mScope)
  258. {
  259. flags = flag;
  260. meth = implMeth;
  261. importName = iName;
  262. importScope = mScope;
  263. tabIx = MDTable.ImplMap;
  264. if (iName == null) flags |= NoMangle;
  265. sortTable = true;
  266. //throw(new NotYetImplementedException("PInvoke "));
  267. }
  268. internal ImplMap(PEReader buff)
  269. {
  270. flags = buff.ReadUInt16();
  271. memForIndex = buff.GetCodedIndex(CIx.MemberForwarded);
  272. importName = buff.GetString();
  273. scopeIx = buff.GetIndex(MDTable.ModuleRef);
  274. sortTable = true;
  275. tabIx = MDTable.ImplMap;
  276. }
  277. internal static void Read(PEReader buff, TableRow[] impls)
  278. {
  279. for (int i = 0; i < impls.Length; i++)
  280. impls[i] = new ImplMap(buff);
  281. }
  282. internal override void Resolve(PEReader buff)
  283. {
  284. meth = (MethodDef)buff.GetCodedElement(CIx.MemberForwarded, memForIndex);
  285. importScope = (ModuleRef)buff.GetElement(MDTable.ModuleRef, scopeIx);
  286. if (meth != null) meth.AddPInvokeInfo(this);
  287. }
  288. internal override uint SortKey()
  289. {
  290. return (meth.Row << MetaData.CIxShiftMap[(uint)CIx.MemberForwarded])
  291. | meth.GetCodedIx(CIx.MemberForwarded);
  292. }
  293. internal sealed override void BuildTables(MetaDataOut md)
  294. {
  295. md.AddToTable(MDTable.ImplMap, this);
  296. iNameIx = md.AddToStringsHeap(importName);
  297. importScope.BuildMDTables(md);
  298. }
  299. internal static uint Size(MetaData md)
  300. {
  301. return 2 + md.CodedIndexSize(CIx.MemberForwarded) +
  302. md.StringsIndexSize() + md.TableIndexSize(MDTable.ModuleRef);
  303. }
  304. internal sealed override void Write(PEWriter output)
  305. {
  306. output.Write(flags);
  307. output.WriteCodedIndex(CIx.MemberForwarded, meth);
  308. output.StringsIndex(iNameIx);
  309. output.WriteIndex(MDTable.ModuleRef, importScope.Row);
  310. }
  311. }
  312. /**************************************************************************/
  313. /// <summary>
  314. /// Base class for field/methods (member of a class)
  315. /// </summary>
  316. public abstract class Member : MetaDataElement
  317. {
  318. protected string name;
  319. protected uint nameIx = 0, sigIx = 0;
  320. protected byte[] signature;
  321. protected uint parentIx = 0;
  322. protected Class parent;
  323. /*-------------------- Constructors ---------------------------------*/
  324. internal Member(string memName, Class paren)
  325. {
  326. name = memName;
  327. parent = paren;
  328. tabIx = MDTable.MemberRef;
  329. }
  330. internal Member(uint parenIx, string name, uint sIx)
  331. {
  332. parentIx = parenIx;
  333. this.name = name;
  334. sigIx = sIx;
  335. tabIx = MDTable.MemberRef;
  336. }
  337. internal Member(string name)
  338. {
  339. this.name = name;
  340. tabIx = MDTable.MemberRef;
  341. }
  342. internal static void ReadMember(PEReader buff, TableRow[] members)
  343. {
  344. for (int i = 0; i < members.Length; i++)
  345. {
  346. uint parenIx = buff.GetCodedIndex(CIx.MemberRefParent);
  347. string memName = buff.GetString();
  348. uint sigIx = buff.GetBlobIx();
  349. if (buff.FirstBlobByte(sigIx) == Field.FieldTag) // got a field
  350. members[i] = new FieldRef(parenIx, memName, sigIx);
  351. else
  352. members[i] = new MethodRef(parenIx, memName, sigIx);
  353. }
  354. }
  355. internal virtual Member ResolveParent(PEReader buff) { return null; }
  356. public MetaDataElement GetParent()
  357. {
  358. if (parent == null) return null;
  359. if (parent.isSpecial())
  360. return parent.GetParent();
  361. return parent;
  362. }
  363. internal void SetParent(Class paren)
  364. {
  365. parent = paren;
  366. }
  367. public string Name() { return name; }
  368. public string QualifiedName() { return parent.TypeName() + "." + name; }
  369. internal bool HasName(string name)
  370. {
  371. return (this.name == name);
  372. }
  373. protected void WriteFlags(CILWriter output, uint flags)
  374. {
  375. uint vis = (flags & 0x07); // visibility mask
  376. switch (vis)
  377. {
  378. case 0: output.Write("compilercontrolled "); break;
  379. case 1: output.Write("private "); break;
  380. case 2: output.Write("famandassem "); break;
  381. case 3: output.Write("assembly "); break;
  382. case 4: output.Write("family "); break;
  383. case 5: output.Write("famorassem "); break;
  384. case 6: output.Write("public "); break;
  385. }
  386. if ((flags & (ushort)FieldAttr.Static) != 0)
  387. {
  388. output.Write("static ");
  389. }
  390. if ((flags & (ushort)FieldAttr.Initonly) != 0)
  391. {
  392. if (this is MethodDef)
  393. {
  394. output.Write("final ");
  395. }
  396. else
  397. {
  398. output.Write("initonly ");
  399. }
  400. }
  401. if ((flags & (ushort)FieldAttr.Literal) != 0)
  402. {
  403. if (this is MethodDef)
  404. {
  405. output.Write("virtual ");
  406. }
  407. else
  408. {
  409. output.Write("literal ");
  410. }
  411. }
  412. if ((flags & (ushort)FieldAttr.Notserialized) != 0)
  413. {
  414. if (this is MethodDef)
  415. {
  416. output.Write("hidebysig ");
  417. }
  418. else
  419. {
  420. output.Write("notserialized ");
  421. }
  422. }
  423. if (this is MethodDef)
  424. {
  425. // more flags required here
  426. if ((flags & (ushort)MethAttr.Abstract) != 0)
  427. {
  428. output.Write("abstract ");
  429. }
  430. if ((flags & (ushort)MethAttr.SpecialName) != 0)
  431. {
  432. output.Write("specialname ");
  433. }
  434. if ((flags & (ushort)MethAttr.RTSpecialName) != 0)
  435. {
  436. output.Write("rtspecialname ");
  437. }
  438. }
  439. else
  440. {
  441. // more flags required here
  442. if ((flags & (ushort)FieldAttr.SpecialName) != 0)
  443. {
  444. output.Write("specialname ");
  445. }
  446. if ((flags & (ushort)FieldAttr.RTSpecialName) != 0)
  447. {
  448. output.Write("rtsspecialname ");
  449. }
  450. }
  451. }
  452. internal abstract void WriteType(CILWriter output);
  453. }
  454. /**************************************************************************/
  455. /// <summary>
  456. /// Descriptor for interface implemented by a class
  457. /// </summary>
  458. public class InterfaceImpl : MetaDataElement
  459. {
  460. ClassDef theClass;
  461. Class theInterface;
  462. uint classIx = 0, interfacesIndex = 0;
  463. /*-------------------- Constructors ---------------------------------*/
  464. internal InterfaceImpl(ClassDef theClass, Class theInterface)
  465. {
  466. this.theClass = theClass;
  467. this.theInterface = theInterface;
  468. tabIx = MDTable.InterfaceImpl;
  469. }
  470. internal InterfaceImpl(ClassDef theClass, TableRow theInterface)
  471. {
  472. this.theClass = theClass;
  473. this.theInterface = (Class)theInterface;
  474. tabIx = MDTable.InterfaceImpl;
  475. }
  476. internal InterfaceImpl(PEReader buff)
  477. {
  478. classIx = buff.GetIndex(MDTable.TypeDef);
  479. interfacesIndex = buff.GetCodedIndex(CIx.TypeDefOrRef);
  480. tabIx = MDTable.InterfaceImpl;
  481. }
  482. internal override void Resolve(PEReader buff)
  483. {
  484. theClass = (ClassDef)buff.GetElement(MDTable.TypeDef, classIx);
  485. theInterface = (Class)buff.GetCodedElement(CIx.TypeDefOrRef, interfacesIndex);
  486. theClass.AddImplementedInterface(this);
  487. }
  488. internal static void Read(PEReader buff, TableRow[] impls)
  489. {
  490. for (int i = 0; i < impls.Length; i++)
  491. impls[i] = new InterfaceImpl(buff);
  492. }
  493. internal ClassDef TheClass() { return theClass; }
  494. internal Class TheInterface() { return theInterface; }
  495. internal sealed override void BuildTables(MetaDataOut md)
  496. {
  497. md.AddToTable(MDTable.InterfaceImpl, this);
  498. if (!theInterface.isDef()) theInterface.BuildMDTables(md);
  499. if (theInterface is ClassSpec) md.ConditionalAddTypeSpec(theInterface);
  500. }
  501. internal static uint Size(MetaData md)
  502. {
  503. return md.TableIndexSize(MDTable.TypeDef) +
  504. md.CodedIndexSize(CIx.TypeDefOrRef);
  505. }
  506. internal sealed override void Write(PEWriter output)
  507. {
  508. output.WriteIndex(MDTable.TypeDef, theClass.Row);
  509. output.WriteCodedIndex(CIx.TypeDefOrRef, theInterface);
  510. }
  511. internal sealed override uint GetCodedIx(CIx code) { return 5; }
  512. }
  513. /**************************************************************************/
  514. /// <summary>
  515. /// Descriptor for resources used in this PE file NOT YET IMPLEMENTED
  516. /// </summary>
  517. public class ManifestResource : MetaDataElement
  518. {
  519. private static readonly uint PublicResource = 0x1;
  520. private static readonly uint PrivateResource = 0x2;
  521. string mrName;
  522. MetaDataElement impl; // can be AssemblyRef, ResourceFile or ModuleFile
  523. uint fileOffset = 0;
  524. uint nameIx = 0, implIx = 0;
  525. uint flags = 0;
  526. PEFile pefile;
  527. byte[] resourceBytes;
  528. /*-------------------- Constructors ---------------------------------*/
  529. internal ManifestResource(PEFile pefile, string name, byte[] resBytes, bool isPub)
  530. {
  531. InitResource(pefile, name, isPub);
  532. this.resourceBytes = resBytes;
  533. }
  534. internal ManifestResource(PEFile pefile, string name, MetaDataElement fileRef, uint offset, bool isPub)
  535. {
  536. InitResource(pefile, name, isPub);
  537. impl = fileRef;
  538. fileOffset = offset;
  539. }
  540. internal ManifestResource(PEFile pefile, ManifestResource mres, bool isPub)
  541. {
  542. this.pefile = pefile;
  543. mrName = mres.mrName;
  544. flags = mres.flags;
  545. this.impl = mres.impl;
  546. this.fileOffset = mres.fileOffset;
  547. this.resourceBytes = mres.resourceBytes;
  548. }
  549. internal ManifestResource(PEReader buff)
  550. {
  551. fileOffset = buff.ReadUInt32();
  552. flags = buff.ReadUInt32();
  553. mrName = buff.GetString();
  554. implIx = buff.GetCodedIndex(CIx.Implementation);
  555. tabIx = MDTable.ManifestResource;
  556. }
  557. private void InitResource(PEFile pefile, string name, bool isPub)
  558. {
  559. this.pefile = pefile;
  560. mrName = name;
  561. if (isPub) flags = PublicResource;
  562. else flags = PrivateResource;
  563. tabIx = MDTable.ManifestResource;
  564. }
  565. internal static void Read(PEReader buff, TableRow[] mrs)
  566. {
  567. for (int i = 0; i < mrs.Length; i++)
  568. mrs[i] = new ManifestResource(buff);
  569. }
  570. internal override void Resolve(PEReader buff)
  571. {
  572. impl = buff.GetCodedElement(CIx.Implementation, implIx);
  573. if (impl == null)
  574. {
  575. if (!buff.skipBody)
  576. resourceBytes = buff.GetResource(fileOffset);
  577. }
  578. }
  579. /*------------------------- public set and get methods --------------------------*/
  580. public string Name
  581. {
  582. get { return mrName; }
  583. set { mrName = value; }
  584. }
  585. public byte[] ResourceBytes
  586. {
  587. get { return resourceBytes; }
  588. set { resourceBytes = value; }
  589. }
  590. public AssemblyRef ResourceAssembly
  591. {
  592. get { if (impl is AssemblyRef) return (AssemblyRef)impl; return null; }
  593. set { impl = value; }
  594. }
  595. public ResourceFile ResFile
  596. {
  597. get { if (impl is ResourceFile) return (ResourceFile)impl; return null; }
  598. set { impl = value; }
  599. }
  600. public ModuleRef ResourceModule
  601. {
  602. get { if (impl is ModuleFile) return ((ModuleFile)impl).fileModule; return null; }
  603. set { impl = value.modFile; }
  604. }
  605. public uint FileOffset
  606. {
  607. get { return fileOffset; }
  608. set { fileOffset = value; }
  609. }
  610. public bool IsPublic
  611. {
  612. get
  613. {
  614. return flags == PublicResource;
  615. }
  616. set
  617. {
  618. if (value)
  619. flags = PublicResource;
  620. else
  621. flags = PrivateResource;
  622. }
  623. }
  624. /*----------------------------- internal functions ------------------------------*/
  625. internal sealed override void BuildTables(MetaDataOut md)
  626. {
  627. md.AddToTable(MDTable.ManifestResource, this);
  628. nameIx = md.AddToStringsHeap(mrName);
  629. if (resourceBytes != null)
  630. {
  631. if (impl != null)
  632. throw new Exception("ERROR: Manifest Resource has byte value and file reference");
  633. fileOffset = md.AddResource(resourceBytes);
  634. }
  635. else
  636. {
  637. if (impl == null)
  638. throw new Exception("ERROR: Manifest Resource has no implementation or value");
  639. impl.BuildMDTables(md);
  640. }
  641. }
  642. internal static uint Size(MetaData md)
  643. {
  644. return 8 + md.StringsIndexSize() +
  645. md.CodedIndexSize(CIx.Implementation);
  646. }
  647. internal sealed override void Write(PEWriter output)
  648. {
  649. output.Write(fileOffset);
  650. output.Write(flags);
  651. output.StringsIndex(nameIx);
  652. output.WriteCodedIndex(CIx.Implementation, impl);
  653. }
  654. internal sealed override uint GetCodedIx(CIx code) { return 18; }
  655. }
  656. /**************************************************************************/
  657. /// <summary>
  658. /// Base class for elements in the PropertyMap, EventMap and
  659. /// NestedClass MetaData tables
  660. /// </summary>
  661. public class MapElem : MetaDataElement
  662. {
  663. ClassDef theClass, parent;
  664. uint elemIx, classIx, endIx = 0;
  665. /*-------------------- Constructors ---------------------------------*/
  666. internal MapElem(ClassDef classDef, uint elIx, MDTable tableIx)
  667. {
  668. theClass = classDef;
  669. elemIx = elIx;
  670. tabIx = tableIx;
  671. sortTable = tabIx == MDTable.NestedClass;
  672. }
  673. internal MapElem(ClassDef classDef, ClassDef paren, MDTable tableIx)
  674. {
  675. theClass = classDef;
  676. parent = paren;
  677. tabIx = tableIx;
  678. sortTable = tabIx == MDTable.NestedClass;
  679. }
  680. internal MapElem(PEReader buff, MDTable tab)
  681. {
  682. tabIx = tab;
  683. classIx = buff.GetIndex(MDTable.TypeDef);
  684. elemIx = buff.GetIndex(tab);
  685. sortTable = tabIx == MDTable.NestedClass;
  686. }
  687. internal static void Read(PEReader buff, TableRow[] maps, MDTable tab)
  688. {
  689. if (tab == MDTable.NestedClass)
  690. {
  691. for (int i = 0; i < maps.Length; i++)
  692. {
  693. //maps[i] = new MapElem(buff,tab);
  694. uint nestClassIx = buff.GetIndex(MDTable.TypeDef);
  695. uint enclClassIx = buff.GetIndex(MDTable.TypeDef);
  696. ClassDef parent = (ClassDef)buff.GetElement(MDTable.TypeDef, enclClassIx);
  697. ClassDef nestClass = ((ClassDef)buff.GetElement(MDTable.TypeDef, nestClassIx)).MakeNestedClass(parent);
  698. buff.InsertInTable(MDTable.TypeDef, nestClass.Row, nestClass);
  699. }
  700. }
  701. else
  702. { // event or property map
  703. MapElem prev = new MapElem(buff, tab);
  704. maps[0] = prev;
  705. for (int i = 1; i < maps.Length; i++)
  706. {
  707. maps[i] = new MapElem(buff, tab);
  708. prev.endIx = ((MapElem)maps[i]).elemIx;
  709. prev = (MapElem)maps[i];
  710. }
  711. switch (tab)
  712. {
  713. case MDTable.PropertyMap:
  714. prev.endIx = buff.GetTableSize(MDTable.Property) + 1;
  715. break;
  716. case MDTable.EventMap:
  717. prev.endIx = buff.GetTableSize(MDTable.Event) + 1;
  718. break;
  719. default:
  720. prev.endIx = buff.GetTableSize(tab) + 1;
  721. break;
  722. }
  723. }
  724. }
  725. internal static void ReadNestedClassInfo(PEReader buff, uint num, uint[] parIxs)
  726. {
  727. for (int i = 0; i < parIxs.Length; i++) parIxs[i] = 0;
  728. for (int i = 0; i < num; i++)
  729. {
  730. int ix = (int)buff.GetIndex(MDTable.TypeDef);
  731. parIxs[ix - 1] = buff.GetIndex(MDTable.TypeDef);
  732. }
  733. }
  734. internal override void Resolve(PEReader buff)
  735. {
  736. theClass = (ClassDef)buff.GetElement(MDTable.TypeDef, classIx);
  737. if (tabIx == MDTable.EventMap)
  738. {
  739. for (uint i = elemIx; i < endIx; i++)
  740. theClass.AddEvent((Event)buff.GetElement(MDTable.Event, i));
  741. }
  742. else if (tabIx == MDTable.PropertyMap)
  743. {
  744. for (uint i = elemIx; i < endIx; i++)
  745. theClass.AddProperty((Property)buff.GetElement(MDTable.Property, i));
  746. }
  747. else
  748. { // must be nested class -- already done
  749. //ClassDef parent = (ClassDef)buff.GetElement(MDTable.TypeDef,elemIx);
  750. //parent.MakeNested(theClass);
  751. }
  752. }
  753. internal static uint Size(MetaData md, MDTable tabIx)
  754. {
  755. return md.TableIndexSize(MDTable.TypeDef) + md.TableIndexSize(tabIx);
  756. }
  757. internal override uint SortKey()
  758. {
  759. return theClass.Row;
  760. }
  761. internal sealed override void BuildTables(MetaDataOut md)
  762. {
  763. md.AddToTable(tabIx, this);
  764. }
  765. internal sealed override void Write(PEWriter output)
  766. {
  767. output.WriteIndex(MDTable.TypeDef, theClass.Row);
  768. if (parent != null)
  769. output.WriteIndex(MDTable.TypeDef, parent.Row);
  770. else
  771. output.WriteIndex(tabIx, elemIx);
  772. }
  773. }
  774. /**************************************************************************/
  775. /// <summary>
  776. /// Descriptor for an overriding method (.override)
  777. /// </summary>
  778. public class MethodImpl : MetaDataElement
  779. {
  780. ClassDef parent;
  781. Method header, body;
  782. uint classIx = 0, methBodyIx = 0, methDeclIx = 0;
  783. bool resolved = true;
  784. /*-------------------- Constructors ---------------------------------*/
  785. internal MethodImpl(ClassDef par, Method decl, Method bod)
  786. {
  787. parent = par;
  788. header = decl;
  789. body = bod;
  790. tabIx = MDTable.MethodImpl;
  791. }
  792. internal MethodImpl(PEReader buff, ClassDef par, uint bIx, uint dIx)
  793. {
  794. buffer = buff;
  795. parent = par;
  796. methBodyIx = bIx;
  797. methDeclIx = dIx;
  798. tabIx = MDTable.MethodImpl;
  799. resolved = false;
  800. }
  801. internal MethodImpl(PEReader buff)
  802. {
  803. classIx = buff.GetIndex(MDTable.TypeDef);
  804. methBodyIx = buff.GetCodedIndex(CIx.MethodDefOrRef);
  805. methDeclIx = buff.GetCodedIndex(CIx.MethodDefOrRef);
  806. tabIx = MDTable.MethodImpl;
  807. }
  808. /*internal static MethodImpl[] GetMethodImpls(PEReader buff, ClassDef paren, uint classIx) {
  809. buff.SetElementPosition(MDTable.MethodImpl,0);
  810. ArrayList impls = new ArrayList();
  811. for (int i=0; i < buff.GetTableSize(MDTable.MethodImpl); i++) {
  812. uint cIx = buff.GetIndex(MDTable.TypeDef);
  813. uint bIx = buff.GetCodedIndex(CIx.MethodDefOrRef);
  814. uint dIx = buff.GetCodedIndex(CIx.MethodDefOrRef);
  815. if (cIx == classIx)
  816. paren.AddMethodOverride(new MethodImpl(buff,paren,bIx,dIx));
  817. }
  818. return (MethodImpl[])impls.ToArray(typeof(MethodImpl));
  819. }
  820. */
  821. public Method Body
  822. {
  823. get
  824. {
  825. if ((body == null) && (methBodyIx != 0))
  826. body = (Method)buffer.GetCodedElement(CIx.MethodDefOrRef, methBodyIx);
  827. return body;
  828. }
  829. set
  830. {
  831. body = value;
  832. if ((!resolved) && (header != null)) resolved = true;
  833. }
  834. }
  835. public Method Header
  836. {
  837. get
  838. {
  839. if ((header == null) && (methDeclIx != 0))
  840. header = (Method)buffer.GetCodedElement(CIx.MethodDefOrRef, methDeclIx);
  841. return header;
  842. }
  843. set
  844. {
  845. header = value;
  846. if ((!resolved) && (body != null)) resolved = true;
  847. }
  848. }
  849. internal void SetOwner(ClassDef cl)
  850. {
  851. parent = cl;
  852. }
  853. internal static void Read(PEReader buff, TableRow[] impls)
  854. {
  855. for (int i = 0; i < impls.Length; i++)
  856. impls[i] = new MethodImpl(buff);
  857. }
  858. internal override void Resolve(PEReader buff)
  859. {
  860. body = (Method)buff.GetCodedElement(CIx.MethodDefOrRef, methBodyIx);
  861. header = (Method)buff.GetCodedElement(CIx.MethodDefOrRef, methDeclIx);
  862. parent = (ClassDef)buff.GetElement(MDTable.TypeDef, classIx);
  863. parent.AddMethodImpl(this);
  864. resolved = true;
  865. }
  866. internal void ResolveMethDetails()
  867. {
  868. body = (Method)buffer.GetCodedElement(CIx.MethodDefOrRef, methBodyIx);
  869. header = (Method)buffer.GetCodedElement(CIx.MethodDefOrRef, methDeclIx);
  870. resolved = true;
  871. }
  872. internal void ChangeRefsToDefs(ClassDef newType, ClassDef[] oldTypes)
  873. {
  874. throw new NotYetImplementedException("Merge for MethodImpls");
  875. }
  876. internal sealed override void BuildTables(MetaDataOut md)
  877. {
  878. md.AddToTable(MDTable.MethodImpl, this);
  879. if (!resolved) ResolveMethDetails();
  880. if (body is MethodRef) body.BuildMDTables(md);
  881. if (header is MethodRef) header.BuildMDTables(md);
  882. }
  883. internal static uint Size(MetaData md)
  884. {
  885. return md.TableIndexSize(MDTable.TypeDef) + 2 * md.CodedIndexSize(CIx.MethodDefOrRef);
  886. }
  887. internal sealed override void Write(PEWriter output)
  888. {
  889. output.WriteIndex(MDTable.TypeDef, parent.Row);
  890. output.WriteCodedIndex(CIx.MethodDefOrRef, body);
  891. output.WriteCodedIndex(CIx.MethodDefOrRef, header);
  892. }
  893. }
  894. /**************************************************************************/
  895. /// <summary>
  896. /// Descriptor for Property and Event methods
  897. /// </summary>
  898. public class MethodSemantics : MetaDataElement
  899. {
  900. MethodType type;
  901. MethodDef meth;
  902. Feature eventOrProp;
  903. uint methIx = 0, assocIx = 0;
  904. /*-------------------- Constructors ---------------------------------*/
  905. internal MethodSemantics(MethodType mType, MethodDef method, Feature feature)
  906. {
  907. type = mType;
  908. meth = method;
  909. eventOrProp = feature;
  910. sortTable = true;
  911. tabIx = MDTable.MethodSemantics;
  912. }
  913. internal MethodSemantics(PEReader buff)
  914. {
  915. type = (MethodType)buff.ReadUInt16();
  916. methIx = buff.GetIndex(MDTable.Method);
  917. assocIx = buff.GetCodedIndex(CIx.HasSemantics);
  918. sortTable = true;
  919. tabIx = MDTable.MethodSemantics;
  920. }
  921. internal static void Read(PEReader buff, TableRow[] methSems)
  922. {
  923. for (int i = 0; i < methSems.Length; i++)
  924. methSems[i] = new MethodSemantics(buff);
  925. }
  926. internal override void Resolve(PEReader buff)
  927. {
  928. meth = (MethodDef)buff.GetElement(MDTable.Method, methIx);
  929. eventOrProp = (Feature)buff.GetCodedElement(CIx.HasSemantics, assocIx);
  930. eventOrProp.AddMethod(this);
  931. }
  932. internal MethodType GetMethodType() { return type; }
  933. internal MethodDef GetMethod() { return meth; }
  934. internal override uint SortKey()
  935. {
  936. return meth.Row;
  937. }
  938. internal sealed override void BuildTables(MetaDataOut md)
  939. {
  940. md.AddToTable(MDTable.MethodSemantics, this);
  941. }
  942. internal static uint Size(MetaData md)
  943. {
  944. return 2 + md.TableIndexSize(MDTable.Method) + md.CodedIndexSize(CIx.HasSemantics);
  945. }
  946. internal sealed override void Write(PEWriter output)
  947. {
  948. output.Write((ushort)type);
  949. output.WriteIndex(MDTable.Method, meth.Row);
  950. output.WriteCodedIndex(CIx.HasSemantics, eventOrProp);
  951. }
  952. }
  953. /**************************************************************************/
  954. /// <summary>
  955. /// Descriptor for a parameter of a method defined in this assembly/module
  956. /// </summary>
  957. public class Param : MetaDataElement
  958. {
  959. private static readonly ushort hasDefault = 0x1000;
  960. private static readonly ushort noDefault = 0xEFFF;
  961. private static readonly ushort hasFieldMarshal = 0x2000;
  962. private static readonly ushort noFieldMarshal = 0xDFFF;
  963. protected string pName;
  964. protected uint nameIx = 0;
  965. Type pType;
  966. internal ushort seqNo = 0;
  967. ushort parMode;
  968. Constant defaultVal;
  969. NativeType marshalType;
  970. /*-------------------- Constructors ---------------------------------*/
  971. /// <summary>
  972. /// Create a new parameter for a method
  973. /// </summary>
  974. /// <param name="mode">param mode (in, out, opt)</param>
  975. /// <param name="parName">parameter name</param>
  976. /// <param name="parType">parameter type</param>
  977. public Param(ParamAttr mode, string parName, Type parType)
  978. {
  979. pName = parName;
  980. pType = parType;
  981. parMode = (ushort)mode;
  982. tabIx = MDTable.Param;
  983. }
  984. // EXPERIMENTAL kjg Nov 19 2007
  985. public static Param DefaultParam()
  986. {
  987. return new Param(ParamAttr.Default, "", null);
  988. }
  989. internal Param(PEReader buff)
  990. {
  991. parMode = buff.ReadUInt16();
  992. seqNo = buff.ReadUInt16();
  993. pName = buff.GetString();
  994. tabIx = MDTable.Param;
  995. }
  996. internal static void Read(PEReader buff, TableRow[] pars)
  997. {
  998. for (int i = 0; i < pars.Length; i++)
  999. pars[i] = new Param(buff);
  1000. }
  1001. internal void Resolve(PEReader buff, uint fIx, Type type)
  1002. {
  1003. this.pType = type;
  1004. }
  1005. /// <summary>
  1006. /// Add a default value to this parameter
  1007. /// </summary>
  1008. /// <param name="cVal">the default value for the parameter</param>
  1009. public void AddDefaultValue(Constant cVal)
  1010. {
  1011. defaultVal = cVal;
  1012. parMode |= hasDefault;
  1013. }
  1014. /// <summary>
  1015. /// Get the default constant value for this parameter
  1016. /// </summary>
  1017. /// <returns></returns>
  1018. public Constant GetDefaultValue() { return defaultVal; }
  1019. /// <summary>
  1020. /// Remove the default constant value for this parameter
  1021. /// </summary>
  1022. public void RemoveDefaultValue() { defaultVal = null; parMode &= noDefault; }
  1023. /// <summary>
  1024. /// Add marshalling information about this parameter
  1025. /// </summary>
  1026. public void SetMarshalType(NativeType mType)
  1027. {
  1028. marshalType = mType;
  1029. parMode |= hasFieldMarshal;
  1030. }
  1031. /// <summary>
  1032. /// Get the parameter marshalling information
  1033. /// </summary>
  1034. /// <returns>The native type to marshall to</returns>
  1035. public NativeType GetMarshalType() { return marshalType; }
  1036. /// <summary>
  1037. /// Remove any marshalling information for this parameter
  1038. /// </summary>
  1039. public void RemoveMashalType() { marshalType = null; parMode &= noFieldMarshal; }
  1040. /// <summary>
  1041. /// Get the type of this parameter
  1042. /// </summary>
  1043. public Type GetParType() { return pType; }
  1044. /// <summary>
  1045. /// Set the type of this parameter
  1046. /// </summary>
  1047. public void SetParType(Type parType) { pType = parType; }
  1048. public void AddAttribute(ParamAttr att)
  1049. {
  1050. this.parMode |= (ushort)att;
  1051. }
  1052. public ParamAttr GetAttributes() { return (ParamAttr)parMode; }
  1053. public void SetAttributes(ParamAttr att)
  1054. {
  1055. this.parMode = (ushort)att;
  1056. }
  1057. /// <summary>
  1058. /// Retrieve the name of this parameter
  1059. /// </summary>
  1060. /// <returns>parameter name</returns>
  1061. public string GetName() { return pName; }
  1062. /// <summary>
  1063. /// Set the name of this parameter
  1064. /// </summary>
  1065. /// <param name="nam">parameter name</param>
  1066. public void SetName(string nam) { pName = nam; }
  1067. /*------------------------ internal functions ----------------------------*/
  1068. internal Param Copy(Type paramType)
  1069. {
  1070. return new Param((ParamAttr)parMode, pName, paramType);
  1071. }
  1072. internal sealed override void BuildTables(MetaDataOut md)
  1073. {
  1074. md.AddToTable(MDTable.Param, this);
  1075. nameIx = md.AddToStringsHeap(pName);
  1076. if (defaultVal != null)
  1077. {
  1078. ConstantElem constElem = new ConstantElem(this, defaultVal);
  1079. constElem.BuildMDTables(md);
  1080. }
  1081. if (marshalType != null)
  1082. {
  1083. FieldMarshal marshalInfo = new FieldMarshal(this, marshalType);
  1084. marshalInfo.BuildMDTables(md);
  1085. }
  1086. }
  1087. internal override void BuildCILInfo(CILWriter output)
  1088. {
  1089. pType.BuildCILInfo(output);
  1090. }
  1091. internal void TypeSig(MemoryStream str)
  1092. {
  1093. pType.TypeSig(str);
  1094. }
  1095. internal static uint Size(MetaData md)
  1096. {
  1097. return 4 + md.StringsIndexSize();
  1098. }
  1099. internal sealed override void Write(PEWriter output)
  1100. {
  1101. output.Write(parMode);
  1102. output.Write(seqNo);
  1103. output.StringsIndex(nameIx);
  1104. }
  1105. internal override void Write(CILWriter output)
  1106. {
  1107. pType.WriteType(output);
  1108. output.Write(" " + pName);
  1109. }
  1110. internal sealed override uint GetCodedIx(CIx code)
  1111. {
  1112. switch (code)
  1113. {
  1114. case (CIx.HasCustomAttr): return 4;
  1115. case (CIx.HasConstant): return 1;
  1116. case (CIx.HasFieldMarshal): return 1;
  1117. }
  1118. return 0;
  1119. }
  1120. }
  1121. /**************************************************************************/
  1122. /// <summary>
  1123. /// Summary description for ConstantElem.
  1124. /// </summary>
  1125. internal class ConstantElem : MetaDataElement
  1126. {
  1127. MetaDataElement parent;
  1128. Constant cValue;
  1129. uint valIx = 0, parentIx = 0;
  1130. /*-------------------- Constructors ---------------------------------*/
  1131. internal ConstantElem(MetaDataElement parent, Constant val)
  1132. {
  1133. this.parent = parent;
  1134. cValue = val;
  1135. sortTable = true;
  1136. tabIx = MDTable.Constant;
  1137. }
  1138. internal ConstantElem(PEReader buff)
  1139. {
  1140. byte constType = buff.ReadByte();
  1141. byte pad = buff.ReadByte();
  1142. parentIx = buff.GetCodedIndex(CIx.HasConstant);
  1143. //valIx = buff.GetBlobIx();
  1144. cValue = buff.GetBlobConst(constType);
  1145. sortTable = true;
  1146. tabIx = MDTable.Constant;
  1147. }
  1148. internal override void Resolve(PEReader buff)
  1149. {
  1150. parent = buff.GetCodedElement(CIx.HasConstant, parentIx);
  1151. if (parent != null)
  1152. {
  1153. if (parent is Param) ((Param)parent).AddDefaultValue(cValue);
  1154. else if (parent is FieldDef) ((FieldDef)parent).AddValue(cValue);
  1155. else ((Property)parent).AddInitValue(cValue);
  1156. }
  1157. }
  1158. internal static void Read(PEReader buff, TableRow[] consts)
  1159. {
  1160. for (int i = 0; i < consts.Length; i++)
  1161. consts[i] = new ConstantElem(buff);
  1162. }
  1163. /*----------------------------- internal functions ------------------------------*/
  1164. internal override uint SortKey()
  1165. {
  1166. return (parent.Row << MetaData.CIxShiftMap[(uint)CIx.HasConstant])
  1167. | parent.GetCodedIx(CIx.HasConstant);
  1168. }
  1169. internal sealed override void BuildTables(MetaDataOut md)
  1170. {
  1171. md.AddToTable(MDTable.Constant, this);
  1172. valIx = cValue.GetBlobIndex(md);
  1173. }
  1174. internal static uint Size(MetaData md)
  1175. {
  1176. return 2 + md.CodedIndexSize(CIx.HasConstant) + md.BlobIndexSize();
  1177. }
  1178. internal sealed override void Write(PEWriter output)
  1179. {
  1180. output.Write(cValue.GetTypeIndex());
  1181. output.Write((byte)0);
  1182. output.WriteCodedIndex(CIx.HasConstant, parent);
  1183. output.BlobIndex(valIx);
  1184. }
  1185. }
  1186. /**************************************************************************/
  1187. /// <summary>
  1188. /// Descriptor for security permissions for a class or a method NOT YET IMPLEMENTED
  1189. /// </summary>
  1190. public class DeclSecurity : MetaDataElement
  1191. {
  1192. SecurityAction action;
  1193. MetaDataElement parent;
  1194. uint parentIx = 0, permissionIx;
  1195. byte[] permissionSet;
  1196. /*-------------------- Constructors ---------------------------------*/
  1197. internal DeclSecurity(MetaDataElement paren, SecurityAction act, byte[] perSet)
  1198. {
  1199. parent = paren;
  1200. action = act;
  1201. permissionSet = perSet;
  1202. sortTable = true;
  1203. tabIx = MDTable.DeclSecurity;
  1204. }
  1205. internal DeclSecurity(PEReader buff)
  1206. {
  1207. action = (SecurityAction)buff.ReadUInt16();
  1208. parentIx = buff.GetCodedIndex(CIx.HasDeclSecurity);
  1209. permissionSet = buff.GetBlob();
  1210. sortTable = true;
  1211. tabIx = MDTable.DeclSecurity;
  1212. }
  1213. internal static void Read(PEReader buff, TableRow[] secs)
  1214. {
  1215. for (int i = 0; i < secs.Length; i++)
  1216. secs[i] = new DeclSecurity(buff);
  1217. }
  1218. internal static DeclSecurity FindSecurity(PEReader buff, MetaDataElement paren, uint codedParIx)
  1219. {
  1220. buff.SetElementPosition(MDTable.DeclSecurity, 0);
  1221. for (int i = 0; i < buff.GetTableSize(MDTable.DeclSecurity); i++)
  1222. {
  1223. uint act = buff.ReadUInt16();
  1224. if (buff.GetCodedIndex(CIx.HasDeclSecurity) == codedParIx)
  1225. return new DeclSecurity(paren, (SecurityAction)act, buff.GetBlob());
  1226. uint junk = buff.GetBlobIx();
  1227. }
  1228. return null;
  1229. }
  1230. internal override void Resolve(PEReader buff)
  1231. {
  1232. parent = buff.GetCodedElement(CIx.HasDeclSecurity, parentIx);
  1233. if (parent != null)
  1234. {
  1235. if (parent is ClassDef) ((ClassDef)parent).AddSecurity(this);
  1236. if (parent is Assembly) ((Assembly)parent).AddSecurity(this);
  1237. if (parent is MethodDef) ((MethodDef)parent).AddSecurity(this);
  1238. }
  1239. }
  1240. internal override uint SortKey()
  1241. {
  1242. return (parent.Row << MetaData.CIxShiftMap[(uint)CIx.HasDeclSecurity])
  1243. | parent.GetCodedIx(CIx.HasDeclSecurity);
  1244. }
  1245. internal sealed override void BuildTables(MetaDataOut md)
  1246. {
  1247. md.AddToTable(MDTable.DeclSecurity, this);
  1248. permissionIx = md.AddToBlobHeap(permissionSet);
  1249. }
  1250. internal static uint Size(MetaData md)
  1251. {
  1252. return 2 + md.CodedIndexSize(CIx.HasDeclSecurity) + md.BlobIndexSize();
  1253. }
  1254. internal sealed override void Write(PEWriter output)
  1255. {
  1256. output.Write((UInt16)action); // or should this be 2 bytes??
  1257. output.WriteCodedIndex(CIx.HasDeclSecurity, parent);
  1258. output.BlobIndex(permissionIx);
  1259. }
  1260. }
  1261. }