예제 #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 /// <param name="type">Type</param>
 /// <param name="flags">Flags</param>
 public EventDefUser(UTF8String name, ITypeDefOrRef type, EventAttributes flags)
 {
     this.name       = name;
     this.eventType  = type;
     this.attributes = (int)flags;
 }
예제 #2
0
        /// <summary>
        /// Returns an assembly name string
        /// </summary>
        /// <param name="name">Simple assembly name</param>
        /// <param name="version">Version or <c>null</c></param>
        /// <param name="culture">Culture or <c>null</c></param>
        /// <param name="publicKey">Public key / public key token or <c>null</c></param>
        /// <param name="attributes">Assembly attributes</param>
        /// <returns>An assembly name string</returns>
        internal static string GetAssemblyNameString(UTF8String name, Version version, UTF8String culture, PublicKeyBase publicKey, AssemblyAttributes attributes)
        {
            var sb = new StringBuilder();

            foreach (var c in UTF8String.ToSystemStringOrEmpty(name))
            {
                if (c == ',' || c == '=')
                {
                    sb.Append('\\');
                }
                sb.Append(c);
            }

            if (version != null)
            {
                sb.Append(", Version=");
                sb.Append(CreateVersionWithNoUndefinedValues(version).ToString());
            }

            if ((object)culture != null)
            {
                sb.Append(", Culture=");
                sb.Append(UTF8String.IsNullOrEmpty(culture) ? "neutral" : culture.String);
            }

            sb.Append(", ");
            sb.Append(publicKey == null || publicKey is PublicKeyToken ? "PublicKeyToken=" : "PublicKey=");
            sb.Append(publicKey == null ? "null" : publicKey.ToString());

            if ((attributes & AssemblyAttributes.Retargetable) != 0)
            {
                sb.Append(", Retargetable=Yes");
            }

            if ((attributes & AssemblyAttributes.ContentType_Mask) == AssemblyAttributes.ContentType_WindowsRuntime)
            {
                sb.Append(", ContentType=WindowsRuntime");
            }

            return(sb.ToString());
        }
예제 #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 /// <param name="signature">Signature</param>
 public FieldDefUser(UTF8String name, FieldSig signature)
     : this(name, signature, 0)
 {
 }
예제 #4
0
 /// <summary>
 /// Compares two locales (cultures)
 /// </summary>
 /// <param name="a">First</param>
 /// <param name="b">Second</param>
 /// <returns>&lt; 0 if a &lt; b, 0 if a == b, &gt; 0 if a &gt; b</returns>
 internal static int LocaleCompareTo(UTF8String a, string b)
 {
     return(GetCanonicalLocale(a).CompareTo(GetCanonicalLocale(b)));
 }
예제 #5
0
 /// <summary>
 /// Gets the hash code of a locale
 /// </summary>
 /// <param name="a">Value</param>
 /// <returns>The hash code</returns>
 internal static int GetHashCodeLocale(UTF8String a)
 {
     return(GetCanonicalLocale(a).GetHashCode());
 }
예제 #6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Simple name</param>
 /// <param name="version">Version</param>
 /// <param name="publicKey">Public key or public key token</param>
 /// <param name="locale">Locale</param>
 /// <exception cref="ArgumentNullException">If any of the args is invalid</exception>
 public AssemblyRefUser(UTF8String name, Version version, PublicKeyBase publicKey, UTF8String locale)
 {
     if ((object)name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (version == null)
     {
         throw new ArgumentNullException("version");
     }
     if ((object)locale == null)
     {
         throw new ArgumentNullException("locale");
     }
     this.name             = name;
     this.version          = version;
     this.publicKeyOrToken = publicKey;
     this.culture          = locale;
     this.attributes       = (int)(publicKey is PublicKey ? AssemblyAttributes.PublicKey : AssemblyAttributes.None);
 }
예제 #7
0
        private MarshalType Read()
        {
            MarshalType returnValue;

            try
            {
                var        nativeType = (NativeType)reader.ReadByte();
                NativeType nt;
                int        size;
                switch (nativeType)
                {
                case NativeType.FixedSysString:
                    size        = CanRead() ? (int)reader.ReadCompressedUInt32() : -1;
                    returnValue = new FixedSysStringMarshalType(size);
                    break;

                case NativeType.SafeArray:
                    var vt      = CanRead() ? (VariantType)reader.ReadCompressedUInt32() : VariantType.NotInitialized;
                    var udtName = CanRead() ? ReadUTF8String() : null;
                    var udtRef  = (object)udtName == null ? null : TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(udtName), null, gpContext);
                    returnValue = new SafeArrayMarshalType(vt, udtRef);
                    break;

                case NativeType.FixedArray:
                    size        = CanRead() ? (int)reader.ReadCompressedUInt32() : -1;
                    nt          = CanRead() ? (NativeType)reader.ReadCompressedUInt32() : NativeType.NotInitialized;
                    returnValue = new FixedArrayMarshalType(size, nt);
                    break;

                case NativeType.Array:
                    nt = CanRead() ? (NativeType)reader.ReadCompressedUInt32() : NativeType.NotInitialized;
                    int paramNum = CanRead() ? (int)reader.ReadCompressedUInt32() : -1;
                    size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1;
                    int flags = CanRead() ? (int)reader.ReadCompressedUInt32() : -1;
                    returnValue = new ArrayMarshalType(nt, paramNum, size, flags);
                    break;

                case NativeType.CustomMarshaler:
                    var guid              = ReadUTF8String();
                    var nativeTypeName    = ReadUTF8String();
                    var custMarshalerName = ReadUTF8String();
                    var cmRef             = TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(custMarshalerName), new CAAssemblyRefFinder(module), gpContext);
                    var cookie            = ReadUTF8String();
                    returnValue = new CustomMarshalType(guid, nativeTypeName, cmRef, cookie);
                    break;

                case NativeType.IUnknown:
                case NativeType.IDispatch:
                case NativeType.IntF:
                    int iidParamIndex = CanRead() ? (int)reader.ReadCompressedUInt32() : -1;
                    return(new InterfaceMarshalType(nativeType, iidParamIndex));

                default:
                    returnValue = new MarshalType(nativeType);
                    break;
                }
            }
            catch
            {
                returnValue = new RawMarshalType(reader.ReadAllBytes());
            }

            return(returnValue);
        }
예제 #8
0
 public ClassName(string ns, string name, bool isValueType = false)
 {
     this.Namespace   = ns;
     this.Name        = name;
     this.IsValueType = isValueType;
 }
예제 #9
0
 public bool Equals(ClassName other)
 {
     // Don't check IsValueType
     return(UTF8String.Equals(Namespace, other.Namespace) &&
            UTF8String.Equals(Name, other.Name));
 }
예제 #10
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 /// <param name="sequence">Sequence</param>
 /// <param name="flags">Flags</param>
 public ParamDefUser(UTF8String name, ushort sequence, ParamAttributes flags)
 {
     this.name       = name;
     this.sequence   = sequence;
     this.attributes = (int)flags;
 }
예제 #11
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="scope">Scope</param>
 /// <param name="name">Name</param>
 /// <param name="flags">Flags</param>
 public ImplMapUser(ModuleRef scope, UTF8String name, PInvokeAttributes flags)
 {
     this.module     = scope;
     this.name       = name;
     this.attributes = (int)flags;
 }
예제 #12
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 /// <param name="sequence">Sequence</param>
 public ParamDefUser(UTF8String name, ushort sequence)
     : this(name, sequence, 0)
 {
 }
예제 #13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 public ParamDefUser(UTF8String name)
     : this(name, 0)
 {
 }
예제 #14
0
        /// <summary>
        /// Figures out which of two assembly names is closer to another assembly name
        /// </summary>
        /// <param name="requested">Requested assembly name</param>
        /// <param name="a">First</param>
        /// <param name="b">Second</param>
        /// <returns>-1 if both are equally close, 0 if <paramref name="a"/> is closest, 1 if
        /// <paramref name="b"/> is closest</returns>
        public int CompareClosest(IAssembly requested, IAssembly a, IAssembly b)
        {
            if (a == b)
            {
                return(0);
            }
            if (a == null)
            {
                return(!CompareName ? 1 : UTF8String.CaseInsensitiveEquals(requested.Name, b.Name) ? 1 : 0);
            }
            if (b == null)
            {
                return(!CompareName ? 0 : UTF8String.CaseInsensitiveEquals(requested.Name, a.Name) ? 0 : 1);
            }

            // Compare the most important parts first:
            //	1. Assembly simple name
            //	2. Public key token
            //	3. Version
            //	4. Locale
            //	5. Content type

            if (CompareName)
            {
                // If the name only matches one of a or b, return that one.
                bool na = UTF8String.CaseInsensitiveEquals(requested.Name, a.Name);
                bool nb = UTF8String.CaseInsensitiveEquals(requested.Name, b.Name);
                if (na && !nb)
                {
                    return(0);
                }
                if (!na && nb)
                {
                    return(1);
                }
                if (!na && !nb)
                {
                    return(-1);
                }
            }

            if (ComparePublicKeyToken)
            {
                bool pa, pb;
                if (PublicKeyBase.IsNullOrEmpty2(requested.PublicKeyOrToken))
                {
                    // If one of them has a pkt but the other one hasn't, return the one with
                    // no pkt.
                    pa = PublicKeyBase.IsNullOrEmpty2(a.PublicKeyOrToken);
                    pb = PublicKeyBase.IsNullOrEmpty2(b.PublicKeyOrToken);
                }
                else
                {
                    // If one of them has the correct pkt, but the other one has an incorrect
                    // pkt, return the one with the correct pkt.
                    pa = PublicKeyBase.TokenEquals(requested.PublicKeyOrToken, a.PublicKeyOrToken);
                    pb = PublicKeyBase.TokenEquals(requested.PublicKeyOrToken, b.PublicKeyOrToken);
                }
                if (pa && !pb)
                {
                    return(0);
                }
                if (!pa && pb)
                {
                    return(1);
                }
            }

            if (CompareVersion && !Utils.Equals(a.Version, b.Version))
            {
                var rv = Utils.CreateVersionWithNoUndefinedValues(requested.Version);
                if (rv == new Version(0, 0, 0, 0))
                {
                    rv = new Version(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue);
                }
                int va = Utils.CompareTo(a.Version, rv);
                int vb = Utils.CompareTo(b.Version, rv);
                if (va == 0)
                {
                    return(0);   // vb != 0 so return 0
                }
                if (vb == 0)
                {
                    return(1);   // va != 0 so return 1
                }
                if (va > 0 && vb < 0)
                {
                    return(0);
                }
                if (va < 0 && vb > 0)
                {
                    return(1);
                }
                // Now either both a and b's version > req version or both are < req version
                if (va > 0)
                {
                    // a.Version and b.Version > req.Version. Pick the one that is closest.
                    return(Utils.CompareTo(a.Version, b.Version) < 0 ? 0 : 1);
                }
                else
                {
                    // a.Version and b.Version < req.Version. Pick the one that is closest.
                    return(Utils.CompareTo(a.Version, b.Version) > 0 ? 0 : 1);
                }
            }

            if (CompareCulture)
            {
                bool la = Utils.LocaleEquals(requested.Culture, a.Culture);
                bool lb = Utils.LocaleEquals(requested.Culture, b.Culture);
                if (la && !lb)
                {
                    return(0);
                }
                if (!la && lb)
                {
                    return(1);
                }
            }

            if (CompareContentType)
            {
                bool ca = requested.ContentType == a.ContentType;
                bool cb = requested.ContentType == b.ContentType;
                if (ca && !cb)
                {
                    return(0);
                }
                if (!ca && cb)
                {
                    return(1);
                }
            }

            return(-1);
        }
예제 #15
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Simple name</param>
 /// <param name="version">Version</param>
 /// <exception cref="ArgumentNullException">If any of the args is invalid</exception>
 public AssemblyRefUser(UTF8String name, Version version)
     : this(name, version, new PublicKey())
 {
 }
예제 #16
0
 public override int GetHashCode()
 {
     // Don't use IsValueType
     return(UTF8String.GetHashCode(Namespace) ^ UTF8String.GetHashCode(Name));
 }
예제 #17
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Simple name</param>
 /// <param name="version">Version</param>
 /// <param name="publicKey">Public key or public key token</param>
 /// <exception cref="ArgumentNullException">If any of the args is invalid</exception>
 public AssemblyRefUser(UTF8String name, Version version, PublicKeyBase publicKey)
     : this(name, version, publicKey, UTF8String.Empty)
 {
 }
예제 #18
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="name">Name of ref</param>
 public MemberRefUser(ModuleDef module, UTF8String name)
 {
     this.module = module;
     this.name   = name;
 }
예제 #19
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name of file</param>
 /// <param name="flags">Flags</param>
 /// <param name="hashValue">File hash</param>
 public FileDefUser(UTF8String name, FileAttributes flags, byte[] hashValue)
 {
     this.name = name;
     this.attributes = (int)flags;
     this.hashValue = hashValue;
 }
예제 #20
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="name">Name of method ref</param>
 /// <param name="sig">Method sig</param>
 public MemberRefUser(ModuleDef module, UTF8String name, MethodSig sig)
     : this(module, name, sig, null)
 {
 }
예제 #21
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="typeDefId">TypeDef ID</param>
 /// <param name="typeName">Type name</param>
 /// <param name="typeNamespace">Type namespace</param>
 /// <param name="flags">Flags</param>
 /// <param name="implementation">Implementation</param>
 public ExportedTypeUser(ModuleDef module, uint typeDefId, UTF8String typeNamespace, UTF8String typeName, TypeAttributes flags, IImplementation implementation)
 {
     this.module         = module;
     this.typeDefId      = typeDefId;
     this.typeName       = typeName;
     this.typeNamespace  = typeNamespace;
     this.attributes     = (int)flags;
     this.implementation = implementation;
     this.implementation_isInitialized = true;
 }
예제 #22
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="name">Type name</param>
 public TypeRefUser(ModuleDef module, UTF8String name)
     : this(module, UTF8String.Empty, name)
 {
 }
예제 #23
0
 /// <summary>
 /// Compares two locales (cultures)
 /// </summary>
 /// <param name="a">First</param>
 /// <param name="b">Second</param>
 /// <returns><c>true</c> if same, <c>false</c> otherwise</returns>
 internal static bool LocaleEquals(UTF8String a, string b)
 {
     return(LocaleCompareTo(a, b) == 0);
 }
예제 #24
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="namespace">Type namespace</param>
 /// <param name="name">Type name</param>
 public TypeRefUser(ModuleDef module, UTF8String @namespace, UTF8String name)
     : this(module, @namespace, name, null)
 {
 }
예제 #25
0
 private static string GetCanonicalLocale(UTF8String locale)
 {
     return(GetCanonicalLocale(UTF8String.ToSystemStringOrEmpty(locale)));
 }
예제 #26
0
        /// <inheritdoc/>
        public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule)
        {
            if (assembly == null)
            {
                return(null);
            }

            if (EnableFrameworkRedirect && !FindExactMatch)
            {
                FrameworkRedirect.ApplyFrameworkRedirect(ref assembly, sourceModule);
            }

#if THREAD_SAFE
            theLock.EnterWriteLock(); try {
#endif
            AssemblyDef resolvedAssembly = Resolve2(assembly, sourceModule);
            if (resolvedAssembly == null)
            {
                string asmName        = UTF8String.ToSystemStringOrEmpty(assembly.Name);
                string asmNameTrimmed = asmName.Trim();
                if (asmName != asmNameTrimmed)
                {
                    assembly = new AssemblyNameInfo
                    {
                        Name             = asmNameTrimmed,
                        Version          = assembly.Version,
                        PublicKeyOrToken = assembly.PublicKeyOrToken,
                        Culture          = assembly.Culture,
                    };
                    resolvedAssembly = Resolve2(assembly, sourceModule);
                }
            }

            if (resolvedAssembly == null)
            {
                // Make sure we don't search for this assembly again. This speeds up callers who
                // keep asking for this assembly when trying to resolve many different TypeRefs
                cachedAssemblies[GetAssemblyNameKey(assembly)] = null;
                return(null);
            }

            var key1 = GetAssemblyNameKey(resolvedAssembly);
            var key2 = GetAssemblyNameKey(assembly);
            AssemblyDef asm1, asm2;
            cachedAssemblies.TryGetValue(key1, out asm1);
            cachedAssemblies.TryGetValue(key2, out asm2);

            if (asm1 != resolvedAssembly && asm2 != resolvedAssembly)
            {
                // This assembly was just resolved
                if (enableTypeDefCache)
                {
                    foreach (var module in resolvedAssembly.Modules.GetSafeEnumerable())
                    {
                        if (module != null)
                        {
                            module.EnableTypeDefFindCache = true;
                        }
                    }
                }
            }

            bool inserted = false;
            if (!cachedAssemblies.ContainsKey(key1))
            {
                cachedAssemblies.Add(key1, resolvedAssembly);
                inserted = true;
            }
            if (!cachedAssemblies.ContainsKey(key2))
            {
                cachedAssemblies.Add(key2, resolvedAssembly);
                inserted = true;
            }
            if (inserted || asm1 == resolvedAssembly || asm2 == resolvedAssembly)
            {
                return(resolvedAssembly);
            }

            // Dupe assembly. Don't insert it.
            var dupeModule = resolvedAssembly.ManifestModule;
            if (dupeModule != null)
            {
                dupeModule.Dispose();
            }
            return(asm1 ?? asm2);

#if THREAD_SAFE
        }

        finally { theLock.ExitWriteLock(); }
#endif
        }
예제 #27
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 public FieldDefUser(UTF8String name)
     : this(name, null)
 {
 }
예제 #28
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Simple name</param>
 /// <exception cref="ArgumentNullException">If any of the args is invalid</exception>
 public AssemblyRefUser(UTF8String name)
     : this(name, new Version(0, 0, 0, 0))
 {
 }
예제 #29
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 /// <param name="signature">Signature</param>
 /// <param name="attributes">Flags</param>
 public FieldDefUser(UTF8String name, FieldSig signature, FieldAttributes attributes)
 {
     this.name       = name;
     this.signature  = signature;
     this.attributes = (int)attributes;
 }
예제 #30
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name</param>
 /// <param name="type">Type</param>
 public EventDefUser(UTF8String name, ITypeDefOrRef type)
     : this(name, type, 0)
 {
 }