Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Extract pkg in setup to avoid a binary scan (closes #49)
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Mar 2, 2022
1 parent 02323d9 commit cd2ee15
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
31 changes: 20 additions & 11 deletions src/Setup/Setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#include <versionhelpers.h>
#include <string>
#include <functional>
#include <fstream>
#include "unzip.h"
#include "bundle_marker.h"
#include "platform_util.h"

using namespace std;
namespace fs = std::filesystem;

// https://stackoverflow.com/a/874160/184746
bool hasEnding(std::wstring const& fullString, std::wstring const& ending)
Expand Down Expand Up @@ -55,30 +57,35 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
return 0;
}

wstring myPath = util::get_current_process_path();
wstring updaterPath = util::get_temp_file_path(L"exe");
wstring packagePath = util::get_temp_file_path(L"nupkg");
uint8_t* memAddr = 0;

try {
// locate bundled package
BYTE* memAddr = util::mmap_read(myPath, 0);
// locate bundled package and map to memory
memAddr = util::mmap_read(util::get_current_process_path(), 0);
if (!memAddr) {
throw new wstring(L"Unable to map executable to memory");
}

int64_t packageOffset, packageLength;
bundle_marker_t::header_offset(&packageOffset, &packageLength);
BYTE* pkgStart = memAddr + packageOffset;
uint8_t* pkgStart = memAddr + packageOffset;
if (packageOffset == 0 || packageLength == 0) {
util::munmap(memAddr);
util::show_error_dialog(L"An error occurred while running setup. The embedded package was not found. Please contact the application author.");
return 0;
throw wstring(L"The embedded package was not found");
}

// extract Squirrel installer
// extract Squirrel installer from package
std::function<bool(ZIPENTRY&)> endsWithSquirrel([](ZIPENTRY& z) {
return hasEnding(std::wstring(z.name), L"Squirrel.exe");
});
unzipSingleFile(pkgStart, packageLength, updaterPath, endsWithSquirrel);
util::munmap(memAddr);

// extract whole package
std::ofstream(packagePath, std::ios::binary).write((char*)pkgStart, packageLength);

// run installer and forward our command line arguments
wstring cmd = L"\"" + updaterPath + L"\" --setup \"" + myPath + L"\" " + pCmdLine;
wstring cmd = L"\"" + updaterPath + L"\" --setup \"" + packagePath + L"\" " + pCmdLine;
util::wexec(cmd.c_str());
}
catch (wstring wsx) {
Expand All @@ -95,7 +102,9 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
util::show_error_dialog(L"An unknown error occurred while running setup. Please contact the application author.");
}

// clean-up after ourselves
// clean-up resources
if(memAddr) util::munmap(memAddr);
DeleteFile(updaterPath.c_str());
DeleteFile(packagePath.c_str());
return 0;
}
10 changes: 5 additions & 5 deletions src/Update/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Squirrel.SimpleSplat;
using Squirrel.SimpleSplat;
using Squirrel.Json;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -121,8 +121,8 @@ static async Task Setup(string setupPath, bool silentInstall, bool checkInstall)
{
Log.Info($"Extracting bundled app data from '{setupPath}'.");

using var pkgStream = Shared.SetupBundle.ReadPackageBundle(setupPath);
var zp = new ZipPackage(pkgStream, true);
using var fs = File.OpenRead(setupPath);
var zp = new ZipPackage(fs, true);
var appname = zp.ProductName;

if (checkInstall) {
Expand Down Expand Up @@ -224,9 +224,9 @@ static async Task Setup(string setupPath, bool silentInstall, bool checkInstall)

// copy package to directory
string packagePath = Path.Combine(tempFolder, zp.FullReleaseFilename);
pkgStream.Position = 0;
fs.Position = 0;
using (var writeStream = File.Open(packagePath, FileMode.Create, FileAccess.ReadWrite))
pkgStream.CopyTo(writeStream);
fs.CopyTo(writeStream);

// create RELEASES file for UpdateManager to read
var entry = ReleaseEntry.GenerateFromFile(packagePath);
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Update.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Squirrel.Shared\Squirrel.Shared.csproj" />
<ProjectReference Include="..\Squirrel\Squirrel.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit cd2ee15

Please sign in to comment.