Exemplo n.º 1
0
        IEnumerable <string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule)
        {
            var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken);

            if (gacInfo != null && pkt != null)
            {
                string pktString  = pkt.ToString();
                string verString  = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString();
                var cultureString = UTF8String.ToSystemStringOrEmpty(assembly.Culture);
                if (cultureString.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                {
                    cultureString = string.Empty;
                }
                var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name);
                foreach (var subDir in gacInfo.SubDirs)
                {
                    var baseDir = Path.Combine(gacInfo.Path, subDir);
                    baseDir = Path.Combine(baseDir, asmSimpleName);
                    baseDir = Path.Combine(baseDir, string.Format("{0}{1}_{2}_{3}", gacInfo.Prefix, verString, cultureString, pktString));
                    var pathName = Path.Combine(baseDir, asmSimpleName + ".dll");
                    if (File.Exists(pathName))
                    {
                        yield return(pathName);
                    }
                }
            }
        }
        /// <summary>
        /// Gets the hash code of an assembly name
        /// </summary>
        /// <param name="a">Assembly name</param>
        /// <returns>The hash code</returns>
        public int GetHashCode(AssemblyNameInfo a)
        {
            if (a == null)
            {
                return(0);
            }

            int hash = 0;

            if (CompareName)
            {
                hash += UTF8String.GetHashCode(a.Name);
            }
            if (CompareVersion)
            {
                hash += Utils.CreateVersionWithNoUndefinedValues(a.Version).GetHashCode();
            }
            if (ComparePublicKeyToken)
            {
                hash += PublicKeyBase.GetHashCodeToken(a.PublicKeyOrToken);
            }
            if (CompareLocale)
            {
                hash += Utils.GetHashCodeLocale(a.Locale);
            }

            return(hash);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the hash code of an assembly name
        /// </summary>
        /// <param name="a">Assembly name</param>
        /// <returns>The hash code</returns>
        public int GetHashCode(IAssembly a)
        {
            if (a == null)
            {
                return(0);
            }

            int hash = 0;

            if (CompareName)
            {
                hash += UTF8String.GetHashCode(a.Name);
            }
            if (CompareVersion)
            {
                hash += Utils.CreateVersionWithNoUndefinedValues(a.Version).GetHashCode();
            }
            if (ComparePublicKeyToken)
            {
                hash += PublicKeyBase.GetHashCodeToken(a.PublicKeyOrToken);
            }
            if (CompareCulture)
            {
                hash += Utils.GetHashCodeLocale(a.Culture);
            }
            if (CompareContentType)
            {
                hash += (int)a.ContentType;
            }

            return(hash);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Parses a version string
 /// </summary>
 /// <param name="versionString">Version string</param>
 /// <returns>A new <see cref="Version"/> or <c>null</c> if <paramref name="versionString"/>
 /// is an invalid version</returns>
 internal static Version ParseVersion(string versionString)
 {
     try {
         return(Utils.CreateVersionWithNoUndefinedValues(new Version(versionString)));
     }
     catch {
         return(null);
     }
 }
Exemplo n.º 5
0
        private IEnumerable <string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule)
        {
            var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken);

            if (gacInfo != null && pkt != null)
            {
                string pktString  = pkt.ToString();
                string verString  = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString();
                var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name);
                foreach (var subDir in gacInfo.subDirs)
                {
                    var baseDir = Path.Combine(gacInfo.path, subDir);
                    baseDir = Path.Combine(baseDir, asmSimpleName);
                    baseDir = Path.Combine(baseDir, string.Format("{0}{1}__{2}", gacInfo.prefix, verString, pktString));
                    var pathName = Path.Combine(baseDir, asmSimpleName + ".dll");
                    if (File.Exists(pathName))
                    {
                        yield return(pathName);
                    }
                }
            }
        }
        /// <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);
        }
Exemplo n.º 7
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);
        }