Example #1
0
        public static int ShowEmptyDelegates(FileInfo winmd, string[] allowItem, IConsole console)
        {
            HashSet <string> allowTable = new HashSet <string>(allowItem);

            using WinmdUtils w1 = WinmdUtils.LoadFromFile(winmd.FullName);
            bool emptyFound = false;

            foreach (DelegateTypeInfo type in w1.GetTypes().Where(t => t is DelegateTypeInfo))
            {
                if (!type.Parameters.Any())
                {
                    if (allowTable.Contains(type.Name))
                    {
                        continue;
                    }

                    if (!emptyFound)
                    {
                        emptyFound = true;
                        console.Out.Write("Empty delegates detected:\r\n");
                    }

                    console?.Out.Write($"{type.Name}\r\n");
                }
            }


            if (!emptyFound)
            {
                console.Out.Write("No empty delegates found.\r\n");
            }

            return(emptyFound ? -1 : 0);
        }
Example #2
0
        public static int ShowPointersToDelegates(FileInfo winmd, string[] allowItem, IConsole console)
        {
            HashSet <string> allowTable = new HashSet <string>(allowItem);

            using WinmdUtils w1 = WinmdUtils.LoadFromFile(winmd.FullName);
            bool pointersFound = false;

            HashSet <string> delegateNames = new HashSet <string>(w1.GetTypes().Where(t => t is DelegateTypeInfo).Select(d => $"{d.Namespace}.{d.Name}"));

            foreach (var type in w1.GetTypes())
            {
                foreach (var pointerInUse in PointerToOneOfNamesInUse(delegateNames, type))
                {
                    if (allowTable.Contains(pointerInUse.Name))
                    {
                        continue;
                    }

                    if (!pointersFound)
                    {
                        console.Out.Write("Pointers to delegates detected:\r\n");
                        pointersFound = true;
                    }

                    console?.Out.Write($"{pointerInUse.Type},{pointerInUse.Name}\r\n");
                }
            }

            if (!pointersFound)
            {
                console.Out.Write("No pointers to delegates found.\r\n");
            }

            return(pointersFound ? -1 : 0);
        }
Example #3
0
        public static int ShowDuplicateTypes(FileInfo winmd, IConsole console)
        {
            using WinmdUtils w1 = WinmdUtils.LoadFromFile(winmd.FullName);
            Dictionary <string, List <string> > nameToTypes = new Dictionary <string, List <string> >();

            foreach (var type in w1.GetTypes())
            {
                if (type is ClassInfo && type.Name == "Apis")
                {
                    continue;
                }

                var toString = type.ToString();
                if (!nameToTypes.TryGetValue(toString, out var structs))
                {
                    structs = new List <string>();
                    nameToTypes[toString] = structs;
                }

                structs.Add(type.Namespace);
            }

            bool dupsFound = false;

            foreach (var pair in nameToTypes)
            {
                if (pair.Value.Count > 1)
                {
                    if (dupsFound == false)
                    {
                        dupsFound = true;
                        console.Out.Write("Duplicate types detected:\r\n");
                    }

                    pair.Value.Sort();

                    console.Out.Write($"{pair.Key}\r\n");
                    foreach (var type in pair.Value)
                    {
                        console.Out.Write($"  {type}\r\n");
                    }
                }
            }

            if (!dupsFound)
            {
                console.Out.Write("No duplicate types found.\r\n");
            }

            return(dupsFound ? -1 : 0);
        }
Example #4
0
        public static int ShowDuplicateConstants(FileInfo winmd, IConsole console)
        {
            using WinmdUtils w1 = WinmdUtils.LoadFromFile(winmd.FullName);
            Dictionary <string, List <string> > nameToNamespace = new Dictionary <string, List <string> >();

            foreach (ClassInfo apiClass in w1.GetTypes().Where(t => t is ClassInfo && t.Name == "Apis"))
            {
                foreach (var field in apiClass.Fields)
                {
                    if (!nameToNamespace.TryGetValue(field.Name, out var namespaces))
                    {
                        namespaces = new List <string>();
                        nameToNamespace[field.Name] = namespaces;
                    }

                    namespaces.Add(apiClass.Namespace);
                }
            }

            bool dupsFound = false;

            foreach (var pair in nameToNamespace)
            {
                if (pair.Value.Count > 1)
                {
                    if (dupsFound == false)
                    {
                        dupsFound = true;
                        console.Out.Write("Duplicate constants detected:\r\n");
                    }

                    pair.Value.Sort();

                    console?.Out.Write($"{pair.Key}\r\n");
                    foreach (var @namespace in pair.Value)
                    {
                        console?.Out.Write($"  {@namespace}\r\n");
                    }
                }
            }

            if (!dupsFound)
            {
                console.Out.Write("No duplicate constants found.\r\n");
            }

            return(dupsFound ? -1 : 0);
        }
Example #5
0
        public static int ShowDuplicateImports(FileInfo winmd, IConsole console)
        {
            using WinmdUtils w1 = WinmdUtils.LoadFromFile(winmd.FullName);
            Dictionary <string, List <string> > nameToImports = new Dictionary <string, List <string> >();

            foreach (var imp in w1.GetDllImports())
            {
                if (!nameToImports.TryGetValue(imp.Name, out var imports))
                {
                    imports = new List <string>();
                    nameToImports[imp.Name] = imports;
                }

                imports.Add(imp.DeclaringType);
            }

            bool dupsFound = false;

            foreach (var pair in nameToImports)
            {
                if (pair.Value.Count > 1)
                {
                    if (dupsFound == false)
                    {
                        dupsFound = true;
                        console.Out.Write("Duplicated imports detected:\r\n");
                    }

                    pair.Value.Sort();

                    console.Out.Write($"{pair.Key}\r\n");
                    foreach (var imp in pair.Value)
                    {
                        console.Out.Write($"  {imp}\r\n");
                    }
                }
            }

            if (!dupsFound)
            {
                console.Out.Write("No duplicate imports found.\r\n");
            }

            return(dupsFound ? -1 : 0);
        }
Example #6
0
        public static int ShowMissingImports(FileInfo first, FileInfo second, string exclusions, IConsole console)
        {
            int ret = 0;

            using WinmdUtils w1 = WinmdUtils.LoadFromFile(first.FullName);
            HashSet <string> remainingImports = new HashSet <string>();

            foreach (var imp in w1.GetDllImports())
            {
                remainingImports.Add(imp.Name.ToString());
            }

            using WinmdUtils w2 = WinmdUtils.LoadFromFile(second.FullName);
            foreach (var imp in w2.GetDllImports())
            {
                var text = imp.Name.ToString();
                if (remainingImports.Contains(text))
                {
                    remainingImports.Remove(text);
                }
            }

            foreach (var imp in remainingImports)
            {
                if (ret == 0)
                {
                    console.Out.Write("Missing imports detected:\r\n");
                    ret = -1;
                }

                console?.Out.Write($"{imp}\r\n");
            }

            if (ret == 0)
            {
                console.Out.Write("No missing imports found.\r\n");
            }

            return(ret);
        }