MDExternClassElem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /// Descriptor for a class defined in another module of THIS assembly
  26. /// and exported (.class extern)
  27. /// </summary>
  28. internal class ExternClass : MetaDataElement
  29. {
  30. MetaDataElement implementation;
  31. uint flags, typeDefId = 0;
  32. uint implIx = 0, nameIx = 0, nameSpaceIx = 0;
  33. string nameSpace, name;
  34. /*-------------------- Constructors ---------------------------------*/
  35. internal ExternClass(TypeAttr attr, string ns, string name, MetaDataElement paren)
  36. {
  37. flags = (uint)attr;
  38. nameSpace = ns;
  39. this.name = name;
  40. implementation = paren;
  41. tabIx = MDTable.ExportedType;
  42. }
  43. public ExternClass(PEReader buff)
  44. {
  45. flags = buff.ReadUInt32();
  46. typeDefId = buff.ReadUInt32();
  47. name = buff.GetString();
  48. nameSpace = buff.GetString();
  49. implIx = buff.GetCodedIndex(CIx.Implementation);
  50. tabIx = MDTable.ExportedType;
  51. }
  52. internal static void Read(PEReader buff, TableRow[] eClasses)
  53. {
  54. for (int i = 0; i < eClasses.Length; i++)
  55. eClasses[i] = new ExternClass(buff);
  56. }
  57. internal static void GetClassRefs(PEReader buff, TableRow[] eClasses)
  58. {
  59. for (uint i = 0; i < eClasses.Length; i++)
  60. {
  61. uint junk = buff.ReadUInt32();
  62. junk = buff.ReadUInt32();
  63. string name = buff.GetString();
  64. string nameSpace = buff.GetString();
  65. uint implIx = buff.GetCodedIndex(CIx.Implementation);
  66. eClasses[i] = new ClassRef(implIx, nameSpace, name);
  67. eClasses[i].Row = i + 1;
  68. }
  69. }
  70. internal override void Resolve(PEReader buff)
  71. {
  72. implementation = buff.GetCodedElement(CIx.Implementation, implIx);
  73. while (implementation is ExternClass)
  74. implementation = ((ExternClass)implementation).implementation;
  75. ((ModuleFile)implementation).fileModule.AddExternClass(this);
  76. }
  77. internal string NameSpace() { return nameSpace; }
  78. internal string Name() { return name; }
  79. internal sealed override void BuildTables(MetaDataOut md)
  80. {
  81. md.AddToTable(MDTable.ExportedType, this);
  82. nameSpaceIx = md.AddToStringsHeap(nameSpace);
  83. nameIx = md.AddToStringsHeap(name);
  84. if (implementation is ModuleRef)
  85. {
  86. ModuleFile mFile = ((ModuleRef)implementation).modFile;
  87. mFile.BuildMDTables(md);
  88. implementation = mFile;
  89. }
  90. }
  91. internal static uint Size(MetaData md)
  92. {
  93. return 8 + 2 * md.StringsIndexSize() + md.CodedIndexSize(CIx.Implementation);
  94. }
  95. internal sealed override void Write(PEWriter output)
  96. {
  97. output.Write(flags);
  98. output.Write(0);
  99. output.StringsIndex(nameIx);
  100. output.StringsIndex(nameSpaceIx);
  101. output.WriteCodedIndex(CIx.Implementation, implementation);
  102. }
  103. internal sealed override uint GetCodedIx(CIx code)
  104. {
  105. switch (code)
  106. {
  107. case (CIx.HasCustomAttr): return 17;
  108. case (CIx.Implementation): return 2;
  109. }
  110. return 0;
  111. }
  112. }
  113. }