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 | |
---|
29 | using System; |
---|
30 | using System.Collections.Generic; |
---|
31 | using System.IO; |
---|
32 | |
---|
33 | using Mono.Cecil.Cil; |
---|
34 | |
---|
35 | namespace Mono.Cecil.Pdb { |
---|
36 | |
---|
37 | class PdbHelper { |
---|
38 | |
---|
39 | #if !READ_ONLY |
---|
40 | public static SymWriter CreateWriter (ModuleDefinition module, string pdb) |
---|
41 | { |
---|
42 | var writer = new SymWriter (); |
---|
43 | |
---|
44 | if (File.Exists (pdb)) |
---|
45 | File.Delete (pdb); |
---|
46 | |
---|
47 | writer.Initialize (new ModuleMetadata (module), pdb, true); |
---|
48 | |
---|
49 | return writer; |
---|
50 | } |
---|
51 | #endif |
---|
52 | |
---|
53 | public static string GetPdbFileName (string assemblyFileName) |
---|
54 | { |
---|
55 | return Path.ChangeExtension (assemblyFileName, ".pdb"); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | public class PdbReaderProvider : ISymbolReaderProvider { |
---|
60 | |
---|
61 | public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) |
---|
62 | { |
---|
63 | return new PdbReader (File.OpenRead (PdbHelper.GetPdbFileName (fileName))); |
---|
64 | } |
---|
65 | |
---|
66 | public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) |
---|
67 | { |
---|
68 | return new PdbReader (symbolStream); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | #if !READ_ONLY |
---|
73 | |
---|
74 | public class PdbWriterProvider : ISymbolWriterProvider { |
---|
75 | |
---|
76 | public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) |
---|
77 | { |
---|
78 | return new PdbWriter (module, PdbHelper.CreateWriter (module, PdbHelper.GetPdbFileName (fileName))); |
---|
79 | } |
---|
80 | |
---|
81 | public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) |
---|
82 | { |
---|
83 | throw new NotImplementedException (); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | #endif |
---|
88 | |
---|
89 | static class GuidMapping { |
---|
90 | |
---|
91 | static readonly Dictionary<Guid, DocumentLanguage> guid_language = new Dictionary<Guid, DocumentLanguage> (); |
---|
92 | static readonly Dictionary<DocumentLanguage, Guid> language_guid = new Dictionary<DocumentLanguage, Guid> (); |
---|
93 | |
---|
94 | static GuidMapping () |
---|
95 | { |
---|
96 | AddMapping (DocumentLanguage.C, new Guid (0x63a08714, 0xfc37, 0x11d2, 0x90, 0x4c, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1)); |
---|
97 | AddMapping (DocumentLanguage.Cpp, new Guid (0x3a12d0b7, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2)); |
---|
98 | AddMapping (DocumentLanguage.CSharp, new Guid (0x3f5162f8, 0x07c6, 0x11d3, 0x90, 0x53, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1)); |
---|
99 | AddMapping (DocumentLanguage.Basic, new Guid (0x3a12d0b8, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2)); |
---|
100 | AddMapping (DocumentLanguage.Java, new Guid (0x3a12d0b4, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2)); |
---|
101 | AddMapping (DocumentLanguage.Cobol, new Guid (0xaf046cd1, 0xd0e1, 0x11d2, 0x97, 0x7c, 0x0, 0xa0, 0xc9, 0xb4, 0xd5, 0xc)); |
---|
102 | AddMapping (DocumentLanguage.Pascal, new Guid (0xaf046cd2, 0xd0e1, 0x11d2, 0x97, 0x7c, 0x0, 0xa0, 0xc9, 0xb4, 0xd5, 0xc)); |
---|
103 | AddMapping (DocumentLanguage.Cil, new Guid (0xaf046cd3, 0xd0e1, 0x11d2, 0x97, 0x7c, 0x0, 0xa0, 0xc9, 0xb4, 0xd5, 0xc)); |
---|
104 | AddMapping (DocumentLanguage.JScript, new Guid (0x3a12d0b6, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2)); |
---|
105 | AddMapping (DocumentLanguage.Smc, new Guid (0xd9b9f7b, 0x6611, 0x11d3, 0xbd, 0x2a, 0x0, 0x0, 0xf8, 0x8, 0x49, 0xbd)); |
---|
106 | AddMapping (DocumentLanguage.MCpp, new Guid (0x4b35fde8, 0x07c6, 0x11d3, 0x90, 0x53, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1)); |
---|
107 | } |
---|
108 | |
---|
109 | static void AddMapping (DocumentLanguage language, Guid guid) |
---|
110 | { |
---|
111 | guid_language.Add (guid, language); |
---|
112 | language_guid.Add (language, guid); |
---|
113 | } |
---|
114 | |
---|
115 | static readonly Guid type_text = new Guid (0x5a869d0b, 0x6611, 0x11d3, 0xbd, 0x2a, 0x00, 0x00, 0xf8, 0x08, 0x49, 0xbd); |
---|
116 | |
---|
117 | public static DocumentType ToType (this Guid guid) |
---|
118 | { |
---|
119 | if (guid == type_text) |
---|
120 | return DocumentType.Text; |
---|
121 | |
---|
122 | return DocumentType.Other; |
---|
123 | } |
---|
124 | |
---|
125 | public static Guid ToGuid (this DocumentType type) |
---|
126 | { |
---|
127 | if (type == DocumentType.Text) |
---|
128 | return type_text; |
---|
129 | |
---|
130 | return new Guid (); |
---|
131 | } |
---|
132 | |
---|
133 | static readonly Guid hash_md5 = new Guid (0x406ea660, 0x64cf, 0x4c82, 0xb6, 0xf0, 0x42, 0xd4, 0x81, 0x72, 0xa7, 0x99); |
---|
134 | static readonly Guid hash_sha1 = new Guid (0xff1816ec, 0xaa5e, 0x4d10, 0x87, 0xf7, 0x6f, 0x49, 0x63, 0x83, 0x34, 0x60); |
---|
135 | |
---|
136 | public static DocumentHashAlgorithm ToHashAlgorithm (this Guid guid) |
---|
137 | { |
---|
138 | if (guid == hash_md5) |
---|
139 | return DocumentHashAlgorithm.MD5; |
---|
140 | |
---|
141 | if (guid == hash_sha1) |
---|
142 | return DocumentHashAlgorithm.SHA1; |
---|
143 | |
---|
144 | return DocumentHashAlgorithm.None; |
---|
145 | } |
---|
146 | |
---|
147 | public static Guid ToGuid (this DocumentHashAlgorithm hash_algo) |
---|
148 | { |
---|
149 | if (hash_algo == DocumentHashAlgorithm.MD5) |
---|
150 | return hash_md5; |
---|
151 | |
---|
152 | if (hash_algo == DocumentHashAlgorithm.SHA1) |
---|
153 | return hash_sha1; |
---|
154 | |
---|
155 | return new Guid (); |
---|
156 | } |
---|
157 | |
---|
158 | public static DocumentLanguage ToLanguage (this Guid guid) |
---|
159 | { |
---|
160 | DocumentLanguage language; |
---|
161 | if (!guid_language.TryGetValue (guid, out language)) |
---|
162 | return DocumentLanguage.Other; |
---|
163 | |
---|
164 | return language; |
---|
165 | } |
---|
166 | |
---|
167 | public static Guid ToGuid (this DocumentLanguage language) |
---|
168 | { |
---|
169 | Guid guid; |
---|
170 | if (!language_guid.TryGetValue (language, out guid)) |
---|
171 | return new Guid (); |
---|
172 | |
---|
173 | return guid; |
---|
174 | } |
---|
175 | |
---|
176 | static readonly Guid vendor_ms = new Guid (0x994b45c4, 0xe6e9, 0x11d2, 0x90, 0x3f, 0x00, 0xc0, 0x4f, 0xa3, 0x02, 0xa1); |
---|
177 | |
---|
178 | public static DocumentLanguageVendor ToVendor (this Guid guid) |
---|
179 | { |
---|
180 | if (guid == vendor_ms) |
---|
181 | return DocumentLanguageVendor.Microsoft; |
---|
182 | |
---|
183 | return DocumentLanguageVendor.Other; |
---|
184 | } |
---|
185 | |
---|
186 | public static Guid ToGuid (this DocumentLanguageVendor vendor) |
---|
187 | { |
---|
188 | if (vendor == DocumentLanguageVendor.Microsoft) |
---|
189 | return vendor_ms; |
---|
190 | |
---|
191 | return new Guid (); |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | #if !NET_3_5 && !NET_4_0 |
---|
197 | |
---|
198 | namespace System.Runtime.CompilerServices { |
---|
199 | |
---|
200 | [AttributeUsage (AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] |
---|
201 | sealed class ExtensionAttribute : Attribute { |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | #endif |
---|