You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.5 KiB

  1. // Copyright (c) 2018 Daniel Grunwald
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. #nullable enable
  19. using System;
  20. using System.IO;
  21. using System.Reflection;
  22. using System.Reflection.Metadata;
  23. using System.Reflection.Metadata.Ecma335;
  24. using System.Reflection.PortableExecutable;
  25. namespace ICSharpCode.Decompiler.Metadata
  26. {
  27. public enum ResourceType
  28. {
  29. Linked,
  30. Embedded,
  31. AssemblyLinked,
  32. }
  33. public abstract class Resource
  34. {
  35. public virtual ResourceType ResourceType => ResourceType.Embedded;
  36. public virtual ManifestResourceAttributes Attributes => ManifestResourceAttributes.Public;
  37. public abstract string Name { get; }
  38. public abstract Stream? TryOpenStream();
  39. public abstract long? TryGetLength();
  40. }
  41. public class ByteArrayResource : Resource
  42. {
  43. public override string Name { get; }
  44. byte[] data;
  45. public ByteArrayResource(string name, byte[] data)
  46. {
  47. this.Name = name ?? throw new ArgumentNullException(nameof(name));
  48. this.data = data ?? throw new ArgumentNullException(nameof(data));
  49. }
  50. public override Stream TryOpenStream()
  51. {
  52. return new MemoryStream(data);
  53. }
  54. public override long? TryGetLength()
  55. {
  56. return data.Length;
  57. }
  58. }
  59. sealed class MetadataResource : Resource
  60. {
  61. public PEFile Module { get; }
  62. public ManifestResourceHandle Handle { get; }
  63. public bool IsNil => Handle.IsNil;
  64. public MetadataResource(PEFile module, ManifestResourceHandle handle)
  65. {
  66. this.Module = module ?? throw new ArgumentNullException(nameof(module));
  67. this.Handle = handle;
  68. }
  69. public bool Equals(MetadataResource other)
  70. {
  71. return Module == other.Module && Handle == other.Handle;
  72. }
  73. public override bool Equals(object? obj)
  74. {
  75. if (obj is MetadataResource res)
  76. return Equals(res);
  77. return false;
  78. }
  79. public override int GetHashCode()
  80. {
  81. return unchecked(982451629 * Module.GetHashCode() + 982451653 * MetadataTokens.GetToken(Handle));
  82. }
  83. public override string Name => Module.Metadata.GetString(Module.Metadata.GetManifestResource(Handle).Name);
  84. public override ManifestResourceAttributes Attributes => Module.Metadata.GetManifestResource(Handle).Attributes;
  85. public bool HasFlag(ManifestResourceAttributes flag) => (Attributes & flag) == flag;
  86. public override ResourceType ResourceType => GetResourceType();
  87. ResourceType GetResourceType()
  88. {
  89. var resource = Module.Metadata.GetManifestResource(Handle);
  90. if (resource.Implementation.IsNil)
  91. return ResourceType.Embedded;
  92. if (resource.Implementation.Kind == HandleKind.AssemblyReference)
  93. return ResourceType.AssemblyLinked;
  94. return ResourceType.Linked;
  95. }
  96. unsafe bool TryReadResource(out byte* ptr, out long length)
  97. {
  98. ptr = null;
  99. length = 0;
  100. // embedded resources cannot be read from this binary.
  101. if (ResourceType != ResourceType.Embedded)
  102. return false;
  103. // get cor header
  104. var headers = Module.Reader.PEHeaders;
  105. if (headers.CorHeader == null)
  106. return false;
  107. var resources = headers.CorHeader.ResourcesDirectory;
  108. // validate resources directory, GetSectionData throws on negative RVAs
  109. if (resources.RelativeVirtualAddress <= 0)
  110. return false;
  111. var sectionData = Module.Reader.GetSectionData(resources.RelativeVirtualAddress);
  112. // validate section length: we need at least 4 bytes to extract
  113. // the actual length of the resource blob.
  114. if (sectionData.Length < 4)
  115. return false;
  116. var offset = Module.Metadata.GetManifestResource(Handle).Offset;
  117. // validate resource offset
  118. if (offset < 0 || offset > sectionData.Length - 4)
  119. return false;
  120. ptr = sectionData.Pointer + offset;
  121. // get actual length of resource blob.
  122. length = ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24);
  123. return length >= 0 && length <= sectionData.Length;
  124. }
  125. public override unsafe Stream? TryOpenStream()
  126. {
  127. if (!TryReadResource(out var ptr, out var length))
  128. return null;
  129. return new ResourceMemoryStream(Module.Reader, ptr + sizeof(int), length);
  130. }
  131. public unsafe override long? TryGetLength()
  132. {
  133. if (!TryReadResource(out _, out var length))
  134. return null;
  135. return length;
  136. }
  137. }
  138. sealed unsafe class ResourceMemoryStream : UnmanagedMemoryStream
  139. {
  140. #pragma warning disable IDE0052 // Remove unread private members
  141. readonly PEReader peReader;
  142. #pragma warning restore IDE0052 // Remove unread private members
  143. public ResourceMemoryStream(PEReader peReader, byte* data, long length)
  144. : base(data, length, length, FileAccess.Read)
  145. {
  146. // Keep the PEReader alive while the stream in in use.
  147. this.peReader = peReader;
  148. }
  149. }
  150. }