Skip to content

Commit

Permalink
Backport Binary File Skipping to 1.2 (#350)
Browse files Browse the repository at this point in the history
* Add Binary file check to skip.

* Update AnalyzeCommand.cs

* Update AnalyzeCommand.cs

* Update AnalyzeCommand.cs

* Update AnalyzeCommand.cs

* Update AnalyzeCommand.cs
  • Loading branch information
gfs committed May 5, 2021
1 parent 0bc5406 commit 25ac4c1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions AppInspector/Commands/AnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ private bool FileChecksPassed(string filePath, ref LanguageInfo languageInfo, lo
throw;
}

using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

if (IsBinary(fs))
{
return false;
}

return true;
}

Expand Down Expand Up @@ -711,9 +718,46 @@ private bool FileChecksPassed(FileEntry fileEntry, ref LanguageInfo languageInfo
return false;
}

if (IsBinary(fileEntry.Content))
{
return false;
}

return true;
}

private bool IsBinary(Stream fileContents)
{
var numRead = 1;
var span = new Span<byte>(new byte[8192]);
var controlsEncountered = 0;
var maxControlsEncountered = (int)(0.3 * fileContents.Length);
while (numRead > 0)
{
numRead = fileContents.Read(span);
for (var i = 0; i < numRead; i++)
{
var ch = (char)span[i];
if (ch == '\0')
{
fileContents.Position = 0;
return true;
}
else if (char.IsControl(ch) && !char.IsWhiteSpace(ch))
{
if (++controlsEncountered > maxControlsEncountered)
{
fileContents.Position = 0;
return true;
}
}
}

}
fileContents.Position = 0;
return false;
}

/// <summary>
/// Allow callers to exclude files that are not core code files and may otherwise report false positives for matches
/// Does not apply to root scan folder which may be named .\test etc. but to subdirectories only
Expand Down

0 comments on commit 25ac4c1

Please sign in to comment.