Exemple #1
0
        IResolutionScope CreateScopeReference(IAssembly defAsm, ModuleDef defMod)
        {
            if (defAsm == null)
            {
                return(null);
            }
            if (defMod != null && defAsm != null && module.Assembly != null)
            {
                if (UTF8String.CaseInsensitiveEquals(module.Assembly.Name, defAsm.Name))
                {
                    if (UTF8String.CaseInsensitiveEquals(module.Name, defMod.Name))
                    {
                        return(module);
                    }
                    return(module.UpdateRowId(new ModuleRefUser(module, defMod.Name)));
                }
            }
            var pkt = PublicKeyBase.ToPublicKeyToken(defAsm.PublicKeyOrToken);

            if (PublicKeyBase.IsNullOrEmpty2(pkt))
            {
                pkt = null;
            }
            return(module.UpdateRowId(new AssemblyRefUser(defAsm.Name, defAsm.Version, pkt, defAsm.Culture)));
        }
Exemple #2
0
        /// <summary>
        /// Checks whether this assembly is a friend assembly of <paramref name="targetAsm"/>
        /// </summary>
        /// <param name="targetAsm">Target assembly</param>
        public bool IsFriendAssemblyOf(AssemblyDef targetAsm)
        {
            if (targetAsm == null)
            {
                return(false);
            }
            if (this == targetAsm)
            {
                return(true);
            }

            // Both must be unsigned or both must be signed according to the
            // InternalsVisibleToAttribute documentation.
            if (PublicKeyBase.IsNullOrEmpty2(publicKey) != PublicKeyBase.IsNullOrEmpty2(targetAsm.PublicKey))
            {
                return(false);
            }

            foreach (var ca in targetAsm.CustomAttributes.FindAll("System.Runtime.CompilerServices.InternalsVisibleToAttribute"))
            {
                if (ca.ConstructorArguments.Count != 1)
                {
                    continue;
                }
                var arg = ca.ConstructorArguments.Get(0, default(CAArgument));
                if (arg.Type.GetElementType() != ElementType.String)
                {
                    continue;
                }
                var asmName = arg.Value as UTF8String;
                if (UTF8String.IsNull(asmName))
                {
                    continue;
                }

                var asmInfo = new AssemblyNameInfo(asmName);
                if (asmInfo.Name != name)
                {
                    continue;
                }
                if (!PublicKeyBase.IsNullOrEmpty2(publicKey))
                {
                    if (!PublicKey.Equals(asmInfo.PublicKeyOrToken as PublicKey))
                    {
                        continue;
                    }
                }
                else if (!PublicKeyBase.IsNullOrEmpty2(asmInfo.PublicKeyOrToken))
                {
                    continue;
                }

                return(true);
            }

            return(false);
        }
        /// <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 a is closest, 1 if b is closest</returns>
        public int CompareClosest(AssemblyNameInfo requested, AssemblyNameInfo a, AssemblyNameInfo b)
        {
            if (a == b)
            {
                return(0);
            }
            if (a == null)
            {
                return(1);
            }
            if (b == null)
            {
                return(0);
            }

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

            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 (CompareLocale)
            {
                bool la = Utils.LocaleEquals(requested.Locale, a.Locale);
                bool lb = Utils.LocaleEquals(requested.Locale, b.Locale);
                if (la && !lb)
                {
                    return(0);
                }
                if (!la && lb)
                {
                    return(1);
                }
            }

            return(-1);
        }
Exemple #4
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.
                var na = UTF8String.CaseInsensitiveEquals(requested.Name, a.Name);
                var 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);
                }
                var va = Utils.CompareTo(a.Version, rv);
                var 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)
                {
                    return(Utils.CompareTo(a.Version, b.Version) < 0 ? 0 : 1);
                }
                return(Utils.CompareTo(a.Version, b.Version) > 0 ? 0 : 1);
            }

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

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

            return(-1);
        }