- Implemented `chrome-path.js` to define functions for locating Chromium binaries across different platforms and nested directories. - Added `verify-chromium.js` to check for the presence of the Chromium binary and log the results, including candidate paths checked. - The scripts support Linux, Windows, and macOS environments, enhancing the flexibility of Chromium binary detection.
91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace StellaOps.Scanner.Analyzers.Lang.Go.Internal;
|
|
|
|
internal static class GoDwarfReader
|
|
{
|
|
private static readonly byte[] VcsSystemToken = Encoding.UTF8.GetBytes("vcs=");
|
|
private static readonly byte[] VcsRevisionToken = Encoding.UTF8.GetBytes("vcs.revision=");
|
|
private static readonly byte[] VcsModifiedToken = Encoding.UTF8.GetBytes("vcs.modified=");
|
|
private static readonly byte[] VcsTimeToken = Encoding.UTF8.GetBytes("vcs.time=");
|
|
|
|
public static bool TryRead(string path, out GoDwarfMetadata? metadata)
|
|
{
|
|
metadata = null;
|
|
|
|
ReadOnlySpan<byte> data;
|
|
try
|
|
{
|
|
var fileInfo = new FileInfo(path);
|
|
if (!fileInfo.Exists || fileInfo.Length == 0 || fileInfo.Length > 256 * 1024 * 1024)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
data = File.ReadAllBytes(path);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
return false;
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var revision = ExtractValue(data, VcsRevisionToken);
|
|
var modifiedText = ExtractValue(data, VcsModifiedToken);
|
|
var timestamp = ExtractValue(data, VcsTimeToken);
|
|
var system = ExtractValue(data, VcsSystemToken);
|
|
|
|
bool? modified = null;
|
|
if (!string.IsNullOrWhiteSpace(modifiedText))
|
|
{
|
|
if (bool.TryParse(modifiedText, out var parsed))
|
|
{
|
|
modified = parsed;
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(revision) && string.IsNullOrWhiteSpace(system) && modified is null && string.IsNullOrWhiteSpace(timestamp))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
metadata = new GoDwarfMetadata(system, revision, modified, timestamp);
|
|
return true;
|
|
}
|
|
|
|
private static string? ExtractValue(ReadOnlySpan<byte> data, ReadOnlySpan<byte> token)
|
|
{
|
|
var index = data.IndexOf(token);
|
|
if (index < 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var start = index + token.Length;
|
|
var end = start;
|
|
|
|
while (end < data.Length)
|
|
{
|
|
var current = data[end];
|
|
if (current == 0 || current == (byte)'\n' || current == (byte)'\r')
|
|
{
|
|
break;
|
|
}
|
|
|
|
end++;
|
|
}
|
|
|
|
if (end <= start)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return Encoding.UTF8.GetString(data.Slice(start, end - start));
|
|
}
|
|
}
|