PdbHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // PdbHelper.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using Mono.Cecil.Cil;
  32. namespace Mono.Cecil.Pdb
  33. {
  34. class PdbHelper
  35. {
  36. public static string GetPdbFileName(string assemblyFileName)
  37. {
  38. return Path.ChangeExtension(assemblyFileName, ".pdb");
  39. }
  40. }
  41. public class PdbReaderProvider : ISymbolReaderProvider
  42. {
  43. public ISymbolReader GetSymbolReader(ModuleDefinition module, string fileName)
  44. {
  45. return new PdbReader(File.OpenRead(PdbHelper.GetPdbFileName(fileName)));
  46. }
  47. public ISymbolReader GetSymbolReader(ModuleDefinition module, Stream symbolStream)
  48. {
  49. return new PdbReader(symbolStream);
  50. }
  51. }
  52. public class GuidCompare : IEqualityComparer<Guid>
  53. {
  54. public bool Equals(Guid x, Guid y)
  55. {
  56. return x == y;
  57. }
  58. public int GetHashCode(Guid obj)
  59. {
  60. return obj.GetHashCode();
  61. }
  62. }
  63. public class DocumentLanguageCompare : IEqualityComparer<DocumentLanguage>
  64. {
  65. public bool Equals(DocumentLanguage x, DocumentLanguage y)
  66. {
  67. return x == y;
  68. }
  69. public int GetHashCode(DocumentLanguage obj)
  70. {
  71. return obj.GetHashCode();
  72. }
  73. }
  74. public class uintCompare : IEqualityComparer<uint>
  75. {
  76. public bool Equals(uint x, uint y)
  77. {
  78. return x == y;
  79. }
  80. public int GetHashCode(uint obj)
  81. {
  82. return obj.GetHashCode();
  83. }
  84. }
  85. static class GuidMapping
  86. {
  87. static readonly Dictionary<Guid, DocumentLanguage> guid_language = new Dictionary<Guid, DocumentLanguage>(new GuidCompare());
  88. static readonly Dictionary<DocumentLanguage, Guid> language_guid = new Dictionary<DocumentLanguage, Guid>(new DocumentLanguageCompare());
  89. static GuidMapping()
  90. {
  91. AddMapping(DocumentLanguage.C, new Guid(0x63a08714, 0xfc37, 0x11d2, 0x90, 0x4c, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1));
  92. AddMapping(DocumentLanguage.Cpp, new Guid(0x3a12d0b7, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2));
  93. AddMapping(DocumentLanguage.CSharp, new Guid(0x3f5162f8, 0x07c6, 0x11d3, 0x90, 0x53, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1));
  94. AddMapping(DocumentLanguage.Basic, new Guid(0x3a12d0b8, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2));
  95. AddMapping(DocumentLanguage.Java, new Guid(0x3a12d0b4, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2));
  96. AddMapping(DocumentLanguage.Cobol, new Guid(0xaf046cd1, 0xd0e1, 0x11d2, 0x97, 0x7c, 0x0, 0xa0, 0xc9, 0xb4, 0xd5, 0xc));
  97. AddMapping(DocumentLanguage.Pascal, new Guid(0xaf046cd2, 0xd0e1, 0x11d2, 0x97, 0x7c, 0x0, 0xa0, 0xc9, 0xb4, 0xd5, 0xc));
  98. AddMapping(DocumentLanguage.Cil, new Guid(0xaf046cd3, 0xd0e1, 0x11d2, 0x97, 0x7c, 0x0, 0xa0, 0xc9, 0xb4, 0xd5, 0xc));
  99. AddMapping(DocumentLanguage.JScript, new Guid(0x3a12d0b6, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2));
  100. AddMapping(DocumentLanguage.Smc, new Guid(0xd9b9f7b, 0x6611, 0x11d3, 0xbd, 0x2a, 0x0, 0x0, 0xf8, 0x8, 0x49, 0xbd));
  101. AddMapping(DocumentLanguage.MCpp, new Guid(0x4b35fde8, 0x07c6, 0x11d3, 0x90, 0x53, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1));
  102. //AddMapping(DocumentLanguage.FSharp, new Guid(0xab4f38c9, 0xb6e6, 0x43ba, 0xbe, 0x3b, 0x58, 0x08, 0x0b, 0x2c, 0xcc, 0xe3));
  103. }
  104. static void AddMapping(DocumentLanguage language, Guid guid)
  105. {
  106. guid_language.Add(guid, language);
  107. language_guid.Add(language, guid);
  108. }
  109. static readonly Guid type_text = new Guid(0x5a869d0b, 0x6611, 0x11d3, 0xbd, 0x2a, 0x00, 0x00, 0xf8, 0x08, 0x49, 0xbd);
  110. public static DocumentType ToType(Guid guid)
  111. {
  112. if (guid == type_text)
  113. return DocumentType.Text;
  114. return DocumentType.Other;
  115. }
  116. public static Guid ToGuid(DocumentType type)
  117. {
  118. if (type == DocumentType.Text)
  119. return type_text;
  120. return new Guid();
  121. }
  122. static readonly Guid hash_md5 = new Guid(0x406ea660, 0x64cf, 0x4c82, 0xb6, 0xf0, 0x42, 0xd4, 0x81, 0x72, 0xa7, 0x99);
  123. static readonly Guid hash_sha1 = new Guid(0xff1816ec, 0xaa5e, 0x4d10, 0x87, 0xf7, 0x6f, 0x49, 0x63, 0x83, 0x34, 0x60);
  124. public static DocumentHashAlgorithm ToHashAlgorithm(Guid guid)
  125. {
  126. if (guid == hash_md5)
  127. return DocumentHashAlgorithm.MD5;
  128. if (guid == hash_sha1)
  129. return DocumentHashAlgorithm.SHA1;
  130. return DocumentHashAlgorithm.None;
  131. }
  132. public static Guid ToGuid(DocumentHashAlgorithm hash_algo)
  133. {
  134. if (hash_algo == DocumentHashAlgorithm.MD5)
  135. return hash_md5;
  136. if (hash_algo == DocumentHashAlgorithm.SHA1)
  137. return hash_sha1;
  138. return new Guid();
  139. }
  140. public static DocumentLanguage ToLanguage(Guid guid)
  141. {
  142. DocumentLanguage language;
  143. if (!guid_language.TryGetValue(guid, out language))
  144. return DocumentLanguage.Other;
  145. return language;
  146. }
  147. public static Guid ToGuid(DocumentLanguage language)
  148. {
  149. Guid guid;
  150. if (!language_guid.TryGetValue(language, out guid))
  151. return new Guid();
  152. return guid;
  153. }
  154. static readonly Guid vendor_ms = new Guid(0x994b45c4, 0xe6e9, 0x11d2, 0x90, 0x3f, 0x00, 0xc0, 0x4f, 0xa3, 0x02, 0xa1);
  155. public static DocumentLanguageVendor ToVendor(Guid guid)
  156. {
  157. if (guid == vendor_ms)
  158. return DocumentLanguageVendor.Microsoft;
  159. return DocumentLanguageVendor.Other;
  160. }
  161. public static Guid ToGuid(DocumentLanguageVendor vendor)
  162. {
  163. if (vendor == DocumentLanguageVendor.Microsoft)
  164. return vendor_ms;
  165. return new Guid();
  166. }
  167. }
  168. }