Exemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    throw new Exception("Please specify path");
                }

                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                IConfigurationRoot config = builder.Build();
                MaxByteCode = 127;
                TargetPath  = args[0];
                SourceFiles = new List <SourceFileData>();
                ProcessDirectory(new DirectoryInfo(TargetPath));

                var binaryFiles = SourceFiles
                                  .Where(x => x.ByteCounts.HighCodeByteCount > 0 && x.ByteCounts.StandardByteCount / x.ByteCounts.HighCodeByteCount > 0.01).ToList();
                var sourceFiles = SourceFiles
                                  .Where(x => (x.ByteCounts.HighCodeByteCount == 0 ||
                                               (x.ByteCounts.HighCodeByteCount > 0 && x.ByteCounts.StandardByteCount / x.ByteCounts.HighCodeByteCount <= 0.01)) && x.Size > 1024).ToList();

                var byteDataList     = new List <ByteData>();
                var byteAnalysisList = new List <ByteAnalysis>();

                foreach (var item in sourceFiles)
                {
                    var byteAnalysis = new ByteAnalysis();
                    byteAnalysis.FileId = item.FileId;
                    byteAnalysis.ByteData
                        = FileChecker.GetOffenderData(MaxByteCode, item.FilePath);
                    byteAnalysisList.Add(byteAnalysis);
                }

                foreach (var item in sourceFiles.Where(x => byteAnalysisList.Where(y => y.FileId == x.FileId && y.ByteData.Any()).Any()))
                {
                    Console.WriteLine($"FileName: {item.FileName} Size: {(item.Size / 1024).ToString()} Kb --> " +
                                      $"High# {item.ByteCounts.HighCodeByteCount}" +
                                      $"Low# {item.ByteCounts.StandardByteCount}");
                    var itemAnalysis = byteAnalysisList.FirstOrDefault(x => item.FileId == x.FileId);
                    if (itemAnalysis != null)
                    {
                        foreach (var byteData in itemAnalysis.ByteData)
                        {
                            Console.WriteLine($"\tByteCode: {byteData.Byte} Index: {byteData.Index} -->");
                        }
                    }
                }
            }
            catch (UnauthorizedAccessException e)
            {
                ScreenRenderer.WriteError("Unauthorized", e.Message);
            }
            catch (FileNotFoundException e)
            {
                ScreenRenderer.WriteError("File Not Found", e.Message);
            }
            catch (DirectoryNotFoundException e)
            {
                ScreenRenderer.WriteError("Directory Not Found", e.Message);
            }
            catch (Exception e)
            {
                ScreenRenderer.WriteError("Unknown Error", e.Message);
            }
        }