MDFileRefElems.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public abstract class FileRef : MetaDataElement
  25. {
  26. protected static readonly uint HasMetaData = 0x0;
  27. protected static readonly uint HasNoMetaData = 0x1;
  28. protected uint nameIx = 0, hashIx = 0;
  29. protected byte[] hashBytes;
  30. protected string name;
  31. protected bool entryPoint = false;
  32. protected uint flags;
  33. /*-------------------- Constructors ---------------------------------*/
  34. internal FileRef(string name, byte[] hashBytes)
  35. {
  36. this.hashBytes = hashBytes;
  37. this.name = name;
  38. tabIx = MDTable.File;
  39. }
  40. internal FileRef(PEReader buff)
  41. {
  42. flags = buff.ReadUInt32();
  43. name = buff.GetString();
  44. hashBytes = buff.GetBlob();
  45. tabIx = MDTable.File;
  46. }
  47. internal static void Read(PEReader buff, TableRow[] files)
  48. {
  49. for (int i = 0; i < files.Length; i++)
  50. {
  51. uint flags = buff.ReadUInt32();
  52. if (flags == HasMetaData)
  53. files[i] = new ModuleFile(buff.GetString(), buff.GetBlob());
  54. else
  55. files[i] = new ResourceFile(buff.GetString(), buff.GetBlob());
  56. }
  57. }
  58. public string Name() { return name; }
  59. public byte[] GetHash() { return hashBytes; }
  60. internal sealed override void BuildTables(MetaDataOut md)
  61. {
  62. md.AddToTable(MDTable.File, this);
  63. nameIx = md.AddToStringsHeap(name);
  64. hashIx = md.AddToBlobHeap(hashBytes);
  65. if (entryPoint) md.SetEntryPoint(this);
  66. }
  67. internal static uint Size(MetaData md)
  68. {
  69. return 4 + md.StringsIndexSize() + md.BlobIndexSize();
  70. }
  71. internal sealed override void Write(PEWriter output)
  72. {
  73. output.Write(flags);
  74. output.StringsIndex(nameIx);
  75. output.BlobIndex(hashIx);
  76. }
  77. internal sealed override uint GetCodedIx(CIx code)
  78. {
  79. switch (code)
  80. {
  81. case (CIx.HasCustomAttr): return 16;
  82. case (CIx.Implementation): return 0;
  83. }
  84. return 0;
  85. }
  86. }
  87. /**************************************************************************/
  88. /// <summary>
  89. /// Descriptor for a file referenced in THIS assembly/module (.file)
  90. /// </summary>
  91. internal class ModuleFile : FileRef
  92. {
  93. internal ModuleRef fileModule;
  94. internal ModuleFile(string name, byte[] hashBytes, bool entryPoint)
  95. : base(name, hashBytes)
  96. {
  97. flags = HasMetaData;
  98. this.entryPoint = entryPoint;
  99. }
  100. internal ModuleFile(string name, byte[] hashBytes)
  101. : base(name, hashBytes)
  102. {
  103. flags = HasMetaData;
  104. }
  105. internal void SetEntryPoint() { entryPoint = true; }
  106. internal void SetHash(byte[] hashVal) { hashBytes = hashVal; }
  107. }
  108. /**************************************************************************/
  109. /// <summary>
  110. /// Descriptor for a file containing a managed resource
  111. /// </summary>
  112. public class ResourceFile : FileRef
  113. {
  114. static ArrayList files = new ArrayList();
  115. /*-------------------- Constructors ---------------------------------*/
  116. public ResourceFile(string name, byte[] hashValue)
  117. : base(name, hashValue)
  118. {
  119. flags = HasNoMetaData;
  120. files.Add(this);
  121. }
  122. public static ResourceFile GetFile(string name)
  123. {
  124. for (int i = 0; i < files.Count; i++)
  125. {
  126. if (((ResourceFile)files[i]).name.Equals(name))
  127. return (ResourceFile)files[i];
  128. }
  129. return null;
  130. }
  131. }
  132. }