| 1 |
// |
| 2 |
// AssemblyNameReference.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// Jb Evain ([email protected]) |
| 6 |
// |
| 7 |
// (C) 2005 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 |
namespace Mono.Cecil { |
| 30 |
|
| 31 |
using System; |
| 32 |
using System.Collections; |
| 33 |
using System.Globalization; |
| 34 |
using System.Security.Cryptography; |
| 35 |
using System.Text; |
| 36 |
|
| 37 |
using Mono.Cecil.Metadata; |
| 38 |
|
| 39 |
internal class AssemblyNameReference : IMetadataScope, IAnnotationProvider, IReflectionStructureVisitable { |
| 40 |
|
| 41 |
string m_name; |
| 42 |
string m_culture; |
| 43 |
Version m_version; |
| 44 |
AssemblyFlags m_flags; |
| 45 |
byte [] m_publicKey; |
| 46 |
byte [] m_publicKeyToken; |
| 47 |
AssemblyHashAlgorithm m_hashAlgo; |
| 48 |
byte [] m_hash; |
| 49 |
MetadataToken m_token; |
| 50 |
IDictionary m_annotations; |
| 51 |
|
| 52 |
bool m_fullNameDiscarded = true; |
| 53 |
string m_fullName; |
| 54 |
|
| 55 |
public string Name { |
| 56 |
get { return m_name; } |
| 57 |
set { |
| 58 |
m_name = value; |
| 59 |
m_fullNameDiscarded = true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public string Culture { |
| 64 |
get { return m_culture; } |
| 65 |
set { |
| 66 |
m_culture = value; |
| 67 |
m_fullNameDiscarded = true; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public Version Version { |
| 72 |
get { return m_version; } |
| 73 |
set { |
| 74 |
m_version = value; |
| 75 |
m_fullNameDiscarded = true; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public AssemblyFlags Flags { |
| 80 |
get { return m_flags; } |
| 81 |
set { m_flags = value; } |
| 82 |
} |
| 83 |
|
| 84 |
public bool HasPublicKey { |
| 85 |
get { return (m_flags & AssemblyFlags.PublicKey) != 0; } |
| 86 |
set { |
| 87 |
if (value) |
| 88 |
m_flags |= AssemblyFlags.PublicKey; |
| 89 |
else |
| 90 |
m_flags &= ~AssemblyFlags.PublicKey; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public bool IsSideBySideCompatible { |
| 95 |
get { return (m_flags & AssemblyFlags.SideBySideCompatible) != 0; } |
| 96 |
set { |
| 97 |
if (value) |
| 98 |
m_flags |= AssemblyFlags.SideBySideCompatible; |
| 99 |
else |
| 100 |
m_flags &= ~AssemblyFlags.SideBySideCompatible; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public bool IsRetargetable { |
| 105 |
get { return (m_flags & AssemblyFlags.Retargetable) != 0; } |
| 106 |
set { |
| 107 |
if (value) |
| 108 |
m_flags |= AssemblyFlags.Retargetable; |
| 109 |
else |
| 110 |
m_flags &= ~AssemblyFlags.Retargetable; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public byte [] PublicKey { |
| 115 |
get { return m_publicKey; } |
| 116 |
set { |
| 117 |
m_publicKey = value; |
| 118 |
m_publicKeyToken = null; |
| 119 |
m_fullNameDiscarded = true; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public byte [] PublicKeyToken { |
| 124 |
get { |
| 125 |
#if !CF_1_0 |
| 126 |
if ((m_publicKeyToken == null || m_publicKeyToken.Length == 0) && (m_publicKey != null && m_publicKey.Length > 0)) { |
| 127 |
HashAlgorithm ha; |
| 128 |
switch (m_hashAlgo) { |
| 129 |
case AssemblyHashAlgorithm.Reserved: |
| 130 |
ha = MD5.Create (); break; |
| 131 |
default: |
| 132 |
// None default to SHA1 |
| 133 |
ha = SHA1.Create (); break; |
| 134 |
} |
| 135 |
byte [] hash = ha.ComputeHash (m_publicKey); |
| 136 |
// we need the last 8 bytes in reverse order |
| 137 |
m_publicKeyToken = new byte [8]; |
| 138 |
Array.Copy (hash, (hash.Length - 8), m_publicKeyToken, 0, 8); |
| 139 |
Array.Reverse (m_publicKeyToken, 0, 8); |
| 140 |
} |
| 141 |
#endif |
| 142 |
return m_publicKeyToken; |
| 143 |
} |
| 144 |
set { |
| 145 |
m_publicKeyToken = value; |
| 146 |
m_fullNameDiscarded = true; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public string FullName { |
| 151 |
get { |
| 152 |
if (m_fullName != null && !m_fullNameDiscarded) |
| 153 |
return m_fullName; |
| 154 |
|
| 155 |
StringBuilder sb = new StringBuilder (); |
| 156 |
string sep = ", "; |
| 157 |
sb.Append (m_name); |
| 158 |
if (m_version != null) { |
| 159 |
sb.Append (sep); |
| 160 |
sb.Append ("Version="); |
| 161 |
sb.Append (m_version.ToString ()); |
| 162 |
} |
| 163 |
sb.Append (sep); |
| 164 |
sb.Append ("Culture="); |
| 165 |
sb.Append (m_culture == null || m_culture.Length == 0 ? "neutral" : m_culture); |
| 166 |
sb.Append (sep); |
| 167 |
sb.Append ("PublicKeyToken="); |
| 168 |
if (this.PublicKeyToken != null && m_publicKeyToken.Length > 0) { |
| 169 |
for (int i = 0 ; i < m_publicKeyToken.Length ; i++) { |
| 170 |
sb.Append (m_publicKeyToken [i].ToString ("x2")); |
| 171 |
} |
| 172 |
} else { |
| 173 |
sb.Append ("null"); |
| 174 |
} |
| 175 |
m_fullName = sb.ToString (); |
| 176 |
m_fullNameDiscarded = false; |
| 177 |
return m_fullName; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public static AssemblyNameReference Parse (string fullName) |
| 182 |
{ |
| 183 |
if (fullName == null) |
| 184 |
throw new ArgumentNullException ("fullName"); |
| 185 |
if (fullName.Length == 0) |
| 186 |
throw new ArgumentException ("Name can not be empty"); |
| 187 |
|
| 188 |
AssemblyNameReference name = new AssemblyNameReference (); |
| 189 |
string [] tokens = fullName.Split (','); |
| 190 |
for (int i = 0; i < tokens.Length; i++) { |
| 191 |
string token = tokens [i].Trim (); |
| 192 |
|
| 193 |
if (i == 0) { |
| 194 |
name.Name = token; |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
string [] parts = token.Split ('='); |
| 199 |
if (parts.Length != 2) |
| 200 |
throw new ArgumentException ("Malformed name"); |
| 201 |
|
| 202 |
switch (parts [0]) { |
| 203 |
case "Version": |
| 204 |
name.Version = new Version (parts [1]); |
| 205 |
break; |
| 206 |
case "Culture": |
| 207 |
name.Culture = parts [1]; |
| 208 |
break; |
| 209 |
case "PublicKeyToken": |
| 210 |
string pkToken = parts [1]; |
| 211 |
if (pkToken == "null") |
| 212 |
break; |
| 213 |
|
| 214 |
name.PublicKeyToken = new byte [pkToken.Length / 2]; |
| 215 |
for (int j = 0; j < name.PublicKeyToken.Length; j++) { |
| 216 |
name.PublicKeyToken [j] = Byte.Parse (pkToken.Substring (j * 2, 2), NumberStyles.HexNumber); |
| 217 |
} |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return name; |
| 223 |
} |
| 224 |
|
| 225 |
public AssemblyHashAlgorithm HashAlgorithm |
| 226 |
{ |
| 227 |
get { return m_hashAlgo; } |
| 228 |
set { m_hashAlgo = value; } |
| 229 |
} |
| 230 |
|
| 231 |
public virtual byte [] Hash { |
| 232 |
get { return m_hash; } |
| 233 |
set { m_hash = value; } |
| 234 |
} |
| 235 |
|
| 236 |
public MetadataToken MetadataToken { |
| 237 |
get { return m_token; } |
| 238 |
set { m_token = value; } |
| 239 |
} |
| 240 |
|
| 241 |
IDictionary IAnnotationProvider.Annotations { |
| 242 |
get { |
| 243 |
if (m_annotations == null) |
| 244 |
m_annotations = new Hashtable (); |
| 245 |
return m_annotations; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
public AssemblyNameReference () : this (string.Empty, string.Empty, new Version (0, 0, 0, 0)) |
| 250 |
{ |
| 251 |
} |
| 252 |
|
| 253 |
public AssemblyNameReference (string name, string culture, Version version) |
| 254 |
{ |
| 255 |
if (name == null) |
| 256 |
throw new ArgumentNullException ("name"); |
| 257 |
if (culture == null) |
| 258 |
throw new ArgumentNullException ("culture"); |
| 259 |
m_name = name; |
| 260 |
m_culture = culture; |
| 261 |
m_version = version; |
| 262 |
m_hashAlgo = AssemblyHashAlgorithm.None; |
| 263 |
} |
| 264 |
|
| 265 |
public override string ToString () |
| 266 |
{ |
| 267 |
return this.FullName; |
| 268 |
} |
| 269 |
|
| 270 |
public virtual void Accept (IReflectionStructureVisitor visitor) |
| 271 |
{ |
| 272 |
visitor.VisitAssemblyNameReference (this); |
| 273 |
} |
| 274 |
} |
| 275 |
} |