Inline DeltaCompressionDotNet and update to netstandard2.0

This commit is contained in:
Caelan Sayler
2021-08-13 11:40:08 +01:00
parent e31ae57159
commit 0519ca298f
13 changed files with 307 additions and 1 deletions

View File

@@ -26,6 +26,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StubExecutable", "src\StubE
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetCore", "src\NugetCore\NugetCore.csproj", "{4950804F-508C-4A24-A295-FEA43D201D76}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeltaCompressionDotNet", "src\DeltaCompressionDotNet\DeltaCompressionDotNet.csproj", "{514B4247-447E-4FAE-A614-137A421E352A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CIBuild|Any CPU = CIBuild|Any CPU
@@ -434,6 +436,54 @@ Global
{4950804F-508C-4A24-A295-FEA43D201D76}.Release|x64.Build.0 = Release|Any CPU
{4950804F-508C-4A24-A295-FEA43D201D76}.Release|x86.ActiveCfg = Release|Any CPU
{4950804F-508C-4A24-A295-FEA43D201D76}.Release|x86.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|Any CPU.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|Any CPU.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|Mixed Platforms.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|Mixed Platforms.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|x64.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|x64.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|x86.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.CIBuild|x86.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|Any CPU.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|Any CPU.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|Mixed Platforms.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|Mixed Platforms.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|x64.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|x64.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|x86.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Coverage|x86.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|x64.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|x64.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|x86.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Debug|x86.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|Any CPU.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|Any CPU.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|x64.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|x64.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|x86.ActiveCfg = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Debug|x86.Build.0 = Debug|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|Any CPU.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|Any CPU.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|Mixed Platforms.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|x64.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|x64.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|x86.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Mono Release|x86.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|Any CPU.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|x64.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|x64.Build.0 = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|x86.ActiveCfg = Release|Any CPU
{514B4247-447E-4FAE-A614-137A421E352A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>default</LangVersion>
<Nullable>enable</Nullable>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
namespace DeltaCompressionDotNet
{
public interface IDeltaCompression
{
void CreateDelta(string oldFilePath, string newFilePath, string deltaFilePath);
void ApplyDelta(string deltaFilePath, string oldFilePath, string newFilePath);
}
// TODO IDeltaCompressionWithHandles (exclusively PatchAPI)
// TODO IDeltaCompressionWithBuffers (PatchAPI and MSDelta)
}

View File

@@ -0,0 +1,14 @@
namespace DeltaCompressionDotNet.MsDelta
{
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/bb417345.aspx#deltaflagtypeflags
/// </remarks>
internal enum ApplyFlags : long
{
/// <summary>Indicates no special handling.</summary>
None = 0,
/// <summary>Allow MSDelta to apply deltas created using PatchAPI.</summary>
AllowLegacy = 1
}
}

View File

@@ -0,0 +1,14 @@
namespace DeltaCompressionDotNet.MsDelta
{
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/bb417345.aspx#deltaflagtypeflags
/// </remarks>
internal enum CreateFlags : long
{
/// <summary>Indicates no special handling.</summary>
None = 0,
/// <summary>Allow the source, target and delta files to exceed the default size limit.</summary>
IgnoreFileSizeLimit = 1 << 17
};
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Runtime.InteropServices;
namespace DeltaCompressionDotNet.MsDelta
{
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/bb417345.aspx#deltainputstructure
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
internal struct DeltaInput
{
/// <summary>Memory address non-editable input buffer.</summary>
public IntPtr Start;
/// <summary>Size of the memory buffer in bytes.</summary>
public IntPtr Size;
/// <summary>
/// Defines whether MSDelta is allowed to edit the input buffer. If you make the input editable, the buffer will
/// be zeroed at function return. However this will cause most MSDelta functions to use less memory.
/// </summary>
[MarshalAs(UnmanagedType.Bool)] public bool Editable;
}
}

View File

@@ -0,0 +1,16 @@
using System;
namespace DeltaCompressionDotNet.MsDelta
{
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/bb417345.aspx#filetypesets
/// </remarks>
[Flags]
internal enum FileTypeSet : long
{
/// <summary>
/// File type set that includes I386, IA64 and AMD64 Portable Executable (PE) files. Others are treated as raw.
/// </summary>
Executables = 0x0FL
}
}

View File

@@ -0,0 +1,11 @@
namespace DeltaCompressionDotNet.MsDelta
{
internal enum HashAlgId
{
/// <summary>No signature.</summary>
None = 0,
/// <summary>32-bit CRC defined in msdelta.dll.</summary>
Crc32 = 32
}
}

View File

@@ -0,0 +1,29 @@
using System;
using System.ComponentModel;
namespace DeltaCompressionDotNet.MsDelta
{
public sealed class MsDeltaCompression : IDeltaCompression
{
public void CreateDelta(string oldFilePath, string newFilePath, string deltaFilePath)
{
const string? sourceOptionsName = null;
const string? targetOptionsName = null;
var globalOptions = new DeltaInput();
var targetFileTime = IntPtr.Zero;
if (!NativeMethods.CreateDelta(
FileTypeSet.Executables, CreateFlags.IgnoreFileSizeLimit, CreateFlags.None, oldFilePath, newFilePath,
sourceOptionsName, targetOptionsName, globalOptions, targetFileTime, HashAlgId.Crc32, deltaFilePath))
{
throw new Win32Exception();
}
}
public void ApplyDelta(string deltaFilePath, string oldFilePath, string newFilePath)
{
if (!NativeMethods.ApplyDelta(ApplyFlags.AllowLegacy, oldFilePath, deltaFilePath, newFilePath))
throw new Win32Exception();
}
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Runtime.InteropServices;
namespace DeltaCompressionDotNet.MsDelta
{
internal static class NativeMethods
{
/// <summary>
/// The ApplyDelta function use the specified delta and source files to create a new copy of the target file.
/// </summary>
/// <param name="applyFlags">Either DELTA_FLAG_NONE or DELTA_APPLY_FLAG_ALLOW_PA19.</param>
/// <param name="sourceName">The name of the source file to which the delta is to be applied.</param>
/// <param name="deltaName">The name of the delta to be applied to the source file.</param>
/// <param name="targetName">The name of the target file that is to be created.</param>
/// <returns>
/// Returns TRUE on success or FALSE otherwise.
/// </returns>
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/bb417345.aspx#applydeltaaw
/// </remarks>
[DllImport("msdelta.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ApplyDelta(
[MarshalAs(UnmanagedType.I8)] ApplyFlags applyFlags,
string sourceName,
string deltaName,
string targetName);
/// <summary>
/// The CreateDelta function creates a delta from the specified source and target files and write the output delta to the designated file name.
/// </summary>
/// <param name="fileTypeSet">The file type set used for Create.</param>
/// <param name="setFlags">The file type set used for Create.</param>
/// <param name="resetFlags">The file type set used for Create.</param>
/// <param name="sourceName">The file type set used for Create.</param>
/// <param name="targetName">The name of the target against which the source is compared.</param>
/// <param name="sourceOptionsName">Reserved. Pass NULL.</param>
/// <param name="targetOptionsName">Reserved. Pass NULL.</param>
/// <param name="globalOptions">Reserved. Pass a DELTA_INPUT structure with lpStart set to NULL and uSize set to 0.</param>
/// <param name="targetFileTime">The time stamp set on the target file after delta Apply. If NULL, the timestamp of the target file during delta Create will be used.</param>
/// <param name="hashAlgId">ALG_ID of the algorithm to be used to generate the target signature.</param>
/// <param name="deltaName">The name of the delta file to be created.</param>
/// <returns>
/// Returns TRUE on success or FALSE otherwise.
/// </returns>
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/bb417345.aspx#createdeltaaw
/// </remarks>
[DllImport("msdelta.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CreateDelta(
[MarshalAs(UnmanagedType.I8)] FileTypeSet fileTypeSet,
[MarshalAs(UnmanagedType.I8)] CreateFlags setFlags,
[MarshalAs(UnmanagedType.I8)] CreateFlags resetFlags,
string sourceName,
string targetName,
string? sourceOptionsName,
string? targetOptionsName,
DeltaInput globalOptions,
IntPtr targetFileTime,
[MarshalAs(UnmanagedType.U4)] HashAlgId hashAlgId,
string deltaName);
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Runtime.InteropServices;
namespace DeltaCompressionDotNet.PatchApi
{
internal static class NativeMethods
{
/// <summary>
/// The ApplyPatchToFile function applies the specified delta to the specified source file. The output file is saved
/// under the designated new file name.
/// </summary>
/// <param name="patchFileName">The name of the delta to be applied to the source file.</param>
/// <param name="oldFileName">The name of the source file to which the delta is to be applied.</param>
/// <param name="newFileName">The name of the target file that is to be created.</param>
/// <param name="applyOptionFlags">ApplyPatch Flags.</param>
/// <returns>Returns TRUE on success or FALSE otherwise.</returns>
/// <remarks>http://msdn.microsoft.com/en-us/library/bb417345.aspx#applypatchtofileaw</remarks>
[DllImport("mspatcha.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ApplyPatchToFile(
string patchFileName, string oldFileName, string newFileName, uint applyOptionFlags);
/// <summary>
/// The CreatePatchFile function creates a delta from the specified source and target files and write the delta to the
/// designated file name.
/// </summary>
/// <param name="oldFileName">The name of the source file.</param>
/// <param name="newFileName">The name of the target file.</param>
/// <param name="patchFileName">The name of the output delta file.</param>
/// <param name="optionFlags">Creation Flags.</param>
/// <param name="optionData">Not used. Pass NULL. Pointer to a structure of type PATCH_OPTION_DATA.</param>
/// <returns>Returns TRUE on success or FALSE otherwise.</returns>
/// <remarks>http://msdn.microsoft.com/en-us/library/bb417345.aspx#createpatchfileaw</remarks>
[DllImport("mspatchc.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CreatePatchFile(
string oldFileName, string newFileName, string patchFileName, uint optionFlags, IntPtr optionData);
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.ComponentModel;
namespace DeltaCompressionDotNet.PatchApi
{
public sealed class PatchApiCompression : IDeltaCompression
{
public void CreateDelta(string oldFilePath, string newFilePath, string deltaFilePath)
{
const int optionFlags = 0;
var optionData = IntPtr.Zero;
if (!NativeMethods.CreatePatchFile(oldFilePath, newFilePath, deltaFilePath, optionFlags, optionData))
throw new Win32Exception();
}
public void ApplyDelta(string deltaFilePath, string oldFilePath, string newFilePath)
{
const int applyOptionFlags = 0;
if (!NativeMethods.ApplyPatchToFile(deltaFilePath, oldFilePath, newFilePath, applyOptionFlags))
throw new Win32Exception();
}
}
}

View File

@@ -9,13 +9,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DeltaCompressionDotNet" Version="2.0.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="SharpCompress" Version="0.28.3" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DeltaCompressionDotNet\DeltaCompressionDotNet.csproj" />
<ProjectReference Include="..\NugetCore\NugetCore.csproj" />
</ItemGroup>