MDElements.cs 48 KB

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