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