2022-02-09 00:44:56 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
using com.muldersoft.slunkcrypt.gui.utils;
|
|
|
|
|
|
|
|
|
|
namespace com.muldersoft.slunkcrypt.gui.process
|
|
|
|
|
{
|
|
|
|
|
static class ExecutableHelper
|
|
|
|
|
{
|
|
|
|
|
private const string FILENAME_FORMAT = "slunkcrypt-cli-{0}.exe";
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Exception classes
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
public class ExecutableNotFoundException : FileNotFoundException
|
|
|
|
|
{
|
|
|
|
|
public ExecutableNotFoundException(string message, string fileName) : base(message, fileName)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Public methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
public static FileStream GetExecutableFile()
|
|
|
|
|
{
|
|
|
|
|
FileStream executableFile = null;
|
|
|
|
|
string appBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
CPUFeatures cpuFeatures = CPUFeatures.Features;
|
2022-05-23 22:38:24 +02:00
|
|
|
|
if (cpuFeatures.cpuArch.Equals(CPUFeatures.CPUArch.a64))
|
|
|
|
|
{
|
|
|
|
|
if (CheckExecutableFile(ref executableFile, appBaseDirectory, "arm64"))
|
|
|
|
|
{
|
|
|
|
|
Trace.Assert(executableFile != null);
|
|
|
|
|
return executableFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (cpuFeatures.cpuArch.Equals(CPUFeatures.CPUArch.x64))
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
2022-04-18 18:52:55 +02:00
|
|
|
|
if (cpuFeatures.hasAVX2 && CheckExecutableFile(ref executableFile, appBaseDirectory, "avx2"))
|
|
|
|
|
{
|
|
|
|
|
Trace.Assert(executableFile != null);
|
|
|
|
|
return executableFile;
|
|
|
|
|
}
|
2022-05-23 22:38:24 +02:00
|
|
|
|
if (CheckExecutableFile(ref executableFile, appBaseDirectory, "x64"))
|
2022-04-18 18:52:55 +02:00
|
|
|
|
{
|
|
|
|
|
Trace.Assert(executableFile != null);
|
|
|
|
|
return executableFile;
|
|
|
|
|
}
|
2022-02-09 00:44:56 +01:00
|
|
|
|
}
|
2022-04-18 18:52:55 +02:00
|
|
|
|
if (cpuFeatures.hasSSE2 && CheckExecutableFile(ref executableFile, appBaseDirectory, "sse2"))
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
|
|
|
|
Trace.Assert(executableFile != null);
|
|
|
|
|
return executableFile;
|
|
|
|
|
}
|
2022-05-23 22:38:24 +02:00
|
|
|
|
if (CheckExecutableFile(ref executableFile, appBaseDirectory, "i686"))
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
|
|
|
|
Trace.Assert(executableFile != null);
|
|
|
|
|
return executableFile;
|
|
|
|
|
}
|
|
|
|
|
throw new ExecutableNotFoundException("SlunkCrypt executable file not found!", FILENAME_FORMAT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Internal methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
private static bool CheckExecutableFile(ref FileStream executableFile, string appBaseDirectory, string suffix)
|
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
bool success = false;
|
2022-02-09 00:44:56 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
executableFile = new FileStream(Path.Combine(appBaseDirectory, String.Format(FILENAME_FORMAT, suffix)), FileMode.Open, FileAccess.Read, FileShare.Read);
|
2022-02-10 22:55:52 +01:00
|
|
|
|
try
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(executableFile.Name);
|
2022-09-29 00:57:03 +02:00
|
|
|
|
Version appVersion = VersionInfo.Version;
|
2022-02-10 22:55:52 +01:00
|
|
|
|
if (string.Equals(fileVersion.FileDescription, "SlunkCrypt", StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
string.Equals(fileVersion.CompanyName, "Muldersoft", StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
(fileVersion.FileMajorPart == appVersion.Major) && (fileVersion.FileMinorPart == appVersion.Minor))
|
|
|
|
|
{
|
2022-09-29 00:57:03 +02:00
|
|
|
|
success = (fileVersion.FilePrivatePart >= appVersion.Revision);
|
2022-02-10 22:55:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
executableFile.Dispose(); /*clean-up*/
|
|
|
|
|
}
|
2022-02-09 00:44:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
2022-02-10 22:55:52 +01:00
|
|
|
|
return success;
|
2022-02-09 00:44:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|