diff --git a/.vscode/settings.json b/.vscode/settings.json index 577e608..ff7d59f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,4 +13,11 @@ "editor.formatOnType": true }, "prettier.proseWrap": "always", + "cSpell.words": [ + "Diagnoser", + "Lzma", + "Pleosoft", + "VCDIFF", + "Xdelta" + ], } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index dbaf89d..37e927e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -18,7 +18,7 @@ "command": "dotnet", "args": [ "cake", - "--target=Build" + "--target=CleanBuild" ], "group": { "kind": "build", diff --git a/build.cake b/build.cake index 638502a..50fa827 100644 --- a/build.cake +++ b/build.cake @@ -14,6 +14,20 @@ Task("Define-Project") info.PreviewNuGetFeed = "https://pkgs.dev.azure.com/pleonex/Pleosoft/_packaging/Pleosoft-Preview/nuget/v3/index.json"; }); +Task("Clean") + .Does(info => +{ + var settings = new DotNetCoreCleanSettings { + Configuration = info.Configuration, + }; + DotNetCoreClean(info.SolutionFile, settings); +}); + +Task("CleanBuild") + .IsDependentOn("Define-Project") + .IsDependentOn("Clean") + .IsDependentOn("Build"); + Task("Default") .IsDependentOn("Stage-Artifacts"); diff --git a/docs/toc.yml b/docs/toc.yml index 313b2bf..cd6c288 100644 --- a/docs/toc.yml +++ b/docs/toc.yml @@ -8,4 +8,4 @@ href: dev/ - name: GitHub - href: https://github.com/pleonex/xdelta-template + href: https://github.com/pleonex/xdelta-sharp diff --git a/src/.editorconfig b/src/.editorconfig index 72060bf..3d83178 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -30,7 +30,7 @@ dotnet_style_predefined_type_for_locals_parameters_members = true:warning dotnet_style_predefined_type_for_member_access = true:warning ### Modifier preferences -dotnet_style_require_accessibility_modifiers = for_non_interface_members:warning +dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion dotnet_style_readonly_field = true:warning ### Parentheses preferences @@ -173,16 +173,18 @@ dotnet_diagnostic.SA1633.severity = none # No XML-format header in source files ### SonarAnalyzer dotnet_diagnostic.S1135.severity = suggestion # It's almost inevitable to have TODO but add bug ID +dotnet_diagnostic.CS1591.severity = none # Disable documentation +dotnet_diagnostic.SA0001.severity = none # Disable documentation +dotnet_diagnostic.SA1600.severity = none # Disable documentation +dotnet_diagnostic.SA1602.severity = none # Disable documentation + # Special rules for test projects [*Tests/**] -dotnet_diagnostic.CS1591.severity = none # Disable documentation dotnet_diagnostic.CA1001.severity = none # No need to implement IDisposable in test classes with cleanup. dotnet_diagnostic.CA1034.severity = none # Public types in test classes for testing implementations dotnet_diagnostic.CA1040.severity = none # Empty interfaces for testing dotnet_diagnostic.CA1062.severity = none # No need to validate args in test methods dotnet_diagnostic.CA1305.severity = none # No culture method for quick test code dotnet_diagnostic.CA1307.severity = none # No culture method for quick test code -dotnet_diagnostic.SA0001.severity = none # Disable documentation -dotnet_diagnostic.SA1600.severity = none # Disable documentation dotnet_diagnostic.S2699.severity = none # Assert may be in helper methods dotnet_diagnostic.S3966.severity = none # Dispose twice to test implementation diff --git a/src/Pleosoft.XdeltaSharp.Cli/Pleosoft.XdeltaSharp.Cli.csproj b/src/Pleosoft.XdeltaSharp.Cli/Pleosoft.XdeltaSharp.Cli.csproj index 1d13f26..d75eddd 100644 --- a/src/Pleosoft.XdeltaSharp.Cli/Pleosoft.XdeltaSharp.Cli.csproj +++ b/src/Pleosoft.XdeltaSharp.Cli/Pleosoft.XdeltaSharp.Cli.csproj @@ -4,6 +4,7 @@ Exe XdeltaSharp Decompressor of VCDIFF patch files (RFC3284). + Pleosoft.XdeltaSharp.Cli net5.0 win-x64;linux-x64;osx-x64 diff --git a/src/Pleosoft.XdeltaSharp.Cli/Program.cs b/src/Pleosoft.XdeltaSharp.Cli/Program.cs index c7e521c..81c8a15 100644 --- a/src/Pleosoft.XdeltaSharp.Cli/Program.cs +++ b/src/Pleosoft.XdeltaSharp.Cli/Program.cs @@ -17,25 +17,27 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; -using System.Diagnostics; - -namespace Xdelta.Cli +namespace Pleosoft.XdeltaSharp.Cli { - class MainClass + using System; + using System.Diagnostics; + using System.IO; + using Pleosoft.XdeltaSharp.Decoder; + + public static class Program { public static void Main(string[] args) { if (args.Length != 3) return; - Stopwatch watcher = Stopwatch.StartNew(); + using FileStream source = OpenForRead(args[0]); + using FileStream patch = OpenForRead(args[1]); + using FileStream target = CreateForWriteAndRead(args[2]); - using (FileStream source = OpenForRead(args[0])) - using (FileStream patch = OpenForRead(args[1])) - using (FileStream target = CreateForWriteAndRead(args[2])) - new Decoder(source, patch, target).Run(); + Stopwatch watcher = Stopwatch.StartNew(); + var decoder = new Decoder(source, patch, target); + decoder.Run(); watcher.Stop(); Console.WriteLine("Done in {0}", watcher.Elapsed); diff --git a/src/Pleosoft.XdeltaSharp.PerformanceTests/Adler32Tests.cs b/src/Pleosoft.XdeltaSharp.PerformanceTests/Adler32Tests.cs index da28109..dc8f19b 100644 --- a/src/Pleosoft.XdeltaSharp.PerformanceTests/Adler32Tests.cs +++ b/src/Pleosoft.XdeltaSharp.PerformanceTests/Adler32Tests.cs @@ -17,7 +17,7 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -namespace Xdelta.PerformanceTests +namespace Pleosoft.XdeltaSharp.PerformanceTests { using System.IO; using BenchmarkDotNet.Attributes; @@ -25,7 +25,7 @@ namespace Xdelta.PerformanceTests [MemoryDiagnoser] public class Adler32Tests { - Stream stream; + private Stream stream; [Params(2 * 1024, 512 * 1024, 4 * 1024 * 1024)] public uint Length { get; set; } diff --git a/src/Pleosoft.XdeltaSharp.PerformanceTests/Pleosoft.XdeltaSharp.PerformanceTests.csproj b/src/Pleosoft.XdeltaSharp.PerformanceTests/Pleosoft.XdeltaSharp.PerformanceTests.csproj index fc1328e..ef0be77 100644 --- a/src/Pleosoft.XdeltaSharp.PerformanceTests/Pleosoft.XdeltaSharp.PerformanceTests.csproj +++ b/src/Pleosoft.XdeltaSharp.PerformanceTests/Pleosoft.XdeltaSharp.PerformanceTests.csproj @@ -3,6 +3,7 @@ Exe Performance tests of the library. + Pleosoft.XdeltaSharp.PerformanceTests net5.0 diff --git a/src/Pleosoft.XdeltaSharp.PerformanceTests/Program.cs b/src/Pleosoft.XdeltaSharp.PerformanceTests/Program.cs index 4a75a8a..a6156af 100644 --- a/src/Pleosoft.XdeltaSharp.PerformanceTests/Program.cs +++ b/src/Pleosoft.XdeltaSharp.PerformanceTests/Program.cs @@ -17,9 +17,8 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -namespace Xdelta.PerformanceTests +namespace Pleosoft.XdeltaSharp.PerformanceTests { - using System; using BenchmarkDotNet.Running; public static class Program diff --git a/src/Pleosoft.XdeltaSharp.UnitTests/Adler32Tests.cs b/src/Pleosoft.XdeltaSharp.UnitTests/Adler32Tests.cs index 549ca1a..f1d83e9 100644 --- a/src/Pleosoft.XdeltaSharp.UnitTests/Adler32Tests.cs +++ b/src/Pleosoft.XdeltaSharp.UnitTests/Adler32Tests.cs @@ -17,7 +17,7 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -namespace Xdelta.UnitTests +namespace Pleosoft.XdeltaSharp.UnitTests { using System; using System.IO; @@ -26,7 +26,7 @@ namespace Xdelta.UnitTests [TestFixture] public class Adler32Tests { - const int NMax = 5552; + private const int NMax = 5552; [Test] public void Guards() @@ -121,14 +121,14 @@ public void LengthLikeNMax() [Test] public void BiggerThanNMax() { - using var stream = GetTestStream(NMax * 4 + 100); + using var stream = GetTestStream((NMax * 4) + 100); uint result = Adler32.Run(stream, stream.Length); Assert.That(result, Is.EqualTo(2570494100)); } - Stream GetTestStream(long length) + private Stream GetTestStream(long length) { var stream = new MemoryStream(); var data = new byte[] { 0xCA, 0xFE }; diff --git a/src/Pleosoft.XdeltaSharp.UnitTests/DecoderTests.cs b/src/Pleosoft.XdeltaSharp.UnitTests/DecoderTests.cs index 4272292..5a3c94c 100644 --- a/src/Pleosoft.XdeltaSharp.UnitTests/DecoderTests.cs +++ b/src/Pleosoft.XdeltaSharp.UnitTests/DecoderTests.cs @@ -17,11 +17,12 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System.IO; -using NUnit.Framework; - -namespace Xdelta.UnitTests +namespace Pleosoft.XdeltaSharp.UnitTests { + using System.IO; + using NUnit.Framework; + using Pleosoft.XdeltaSharp.Decoder; + [TestFixture] public class DecoderTests { @@ -30,20 +31,6 @@ public class DecoderTests private Stream patch; private Stream output; - private void InitWithStandardHeader() - { - input = new MemoryStream(); - patch = new MemoryStream(); - output = new MemoryStream(); - - BinaryWriter patchWriter = new BinaryWriter(patch); - patchWriter.Write(0x00C4C3D6); - patchWriter.Write((byte)0x00); - patch.Position = 0; - - decoder = new Decoder(input, patch, output); - } - [TearDown] public void TearDown() { @@ -66,6 +53,19 @@ public void Getters() Assert.AreSame(patch, decoder.Patch); Assert.AreSame(output, decoder.Output); } + + private void InitWithStandardHeader() + { + input = new MemoryStream(); + patch = new MemoryStream(); + output = new MemoryStream(); + + BinaryWriter patchWriter = new BinaryWriter(patch); + patchWriter.Write(0x00C4C3D6); + patchWriter.Write((byte)0x00); + patch.Position = 0; + + decoder = new Decoder(input, patch, output); + } } } - diff --git a/src/Pleosoft.XdeltaSharp.UnitTests/HeaderReaderTests.cs b/src/Pleosoft.XdeltaSharp.UnitTests/HeaderReaderTests.cs index 1a9f779..58b85be 100644 --- a/src/Pleosoft.XdeltaSharp.UnitTests/HeaderReaderTests.cs +++ b/src/Pleosoft.XdeltaSharp.UnitTests/HeaderReaderTests.cs @@ -17,12 +17,13 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using NUnit.Framework; -using System; -using System.IO; - -namespace Xdelta.UnitTests +namespace Pleosoft.XdeltaSharp.UnitTests { + using System; + using System.IO; + using NUnit.Framework; + using Pleosoft.XdeltaSharp.Decoder; + [TestFixture] public class HeaderReaderTests { @@ -35,9 +36,9 @@ public class HeaderReaderTests [SetUp] public void SetUp() { - input = new MemoryStream(); + input = new MemoryStream(); output = new MemoryStream(); - patch = new MemoryStream(); + patch = new MemoryStream(); } [TearDown] @@ -48,19 +49,6 @@ public void TearDown() output.Dispose(); } - private void WriteBytes(params byte[] data) - { - patch.Write(data, 0, data.Length); - patch.Position -= data.Length; - } - - private void TestThrows(string message) - where T : SystemException - { - T exception = Assert.Throws(() => new Decoder(input, patch, output)); - Assert.AreEqual(message, exception.Message); - } - [Test] public void InvalidStamp() { @@ -122,6 +110,18 @@ public void ReadCorrectlyApplicationData() Decoder decoder = new Decoder(input, patch, output); Assert.AreEqual("pleonex", decoder.Header.ApplicationData); } + + private void WriteBytes(params byte[] data) + { + patch.Write(data, 0, data.Length); + patch.Position -= data.Length; + } + + private void TestThrows(string message) + where T : SystemException + { + T exception = Assert.Throws(() => new Decoder(input, patch, output)); + Assert.AreEqual(message, exception.Message); + } } } - diff --git a/src/Pleosoft.XdeltaSharp.UnitTests/Pleosoft.XdeltaSharp.UnitTests.csproj b/src/Pleosoft.XdeltaSharp.UnitTests/Pleosoft.XdeltaSharp.UnitTests.csproj index 676deef..564f0a9 100644 --- a/src/Pleosoft.XdeltaSharp.UnitTests/Pleosoft.XdeltaSharp.UnitTests.csproj +++ b/src/Pleosoft.XdeltaSharp.UnitTests/Pleosoft.XdeltaSharp.UnitTests.csproj @@ -2,6 +2,7 @@ Unit tests of the library. + Pleosoft.XdeltaSharp.UnitTests netcoreapp3.1;net48;net5.0 8.0 diff --git a/src/Pleosoft.XdeltaSharp.UnitTests/VcdReaderTests.cs b/src/Pleosoft.XdeltaSharp.UnitTests/VcdReaderTests.cs index 813e905..111f147 100644 --- a/src/Pleosoft.XdeltaSharp.UnitTests/VcdReaderTests.cs +++ b/src/Pleosoft.XdeltaSharp.UnitTests/VcdReaderTests.cs @@ -17,17 +17,17 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; -using NUnit.Framework; - -namespace Xdelta.UnitTests +namespace Pleosoft.XdeltaSharp.UnitTests { + using System; + using System.IO; + using NUnit.Framework; + [TestFixture] public class VcdReaderTests { - VcdReader reader; - Stream stream; + private VcdReader reader; + private Stream stream; [SetUp] public void SetUp() @@ -42,19 +42,6 @@ public void TearDown() stream.Dispose(); } - private void WriteBytes(params byte[] data) - { - stream.Write(data, 0, data.Length); - stream.Position -= data.Length; - } - - private void TestThrows(TestDelegate code, string message) - where T : SystemException - { - T exception = Assert.Throws(code); - Assert.AreEqual(message, exception.Message); - } - [Test] public void ReadByteWithExactSize() { @@ -123,6 +110,18 @@ public void ReadMoreThanAllowedBytes() () => reader.ReadBytes(0x80000010), "Trying to read more than UInt32.MaxValue bytes"); } + + private void WriteBytes(params byte[] data) + { + stream.Write(data, 0, data.Length); + stream.Position -= data.Length; + } + + private void TestThrows(TestDelegate code, string message) + where T : SystemException + { + T exception = Assert.Throws(code); + Assert.AreEqual(message, exception.Message); + } } } - diff --git a/src/Pleosoft.XdeltaSharp.UnitTests/WindowReaderTests.cs b/src/Pleosoft.XdeltaSharp.UnitTests/WindowReaderTests.cs index 8ec20fa..d794f84 100644 --- a/src/Pleosoft.XdeltaSharp.UnitTests/WindowReaderTests.cs +++ b/src/Pleosoft.XdeltaSharp.UnitTests/WindowReaderTests.cs @@ -17,12 +17,14 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; -using NUnit.Framework; - -namespace Xdelta.UnitTests +namespace Pleosoft.XdeltaSharp.UnitTests { + using System; + using System.IO; + using NUnit.Framework; + using Pleosoft.XdeltaSharp.Decoder; + using Pleosoft.XdeltaSharp.Vcdiff; + [TestFixture] public class WindowReaderTests { @@ -35,9 +37,9 @@ public class WindowReaderTests [SetUp] public void SetUp() { - input = new MemoryStream(); + input = new MemoryStream(); output = new MemoryStream(); - patch = new MemoryStream(); + patch = new MemoryStream(); WriteGenericHeader(); decoder = new Decoder(input, patch, output); @@ -51,36 +53,18 @@ public void TearDown() output.Dispose(); } - private void WriteGenericHeader() - { - WriteBytes(0xD6, 0xC3, 0xC4, 0x00, 0x00); - } - - private void WriteBytes(params byte[] data) - { - patch.Write(data, 0, data.Length); - patch.Position -= data.Length; - } - - private void TestThrows(string message) - where T : SystemException - { - T exception = Assert.Throws(() => decoder.Run()); - Assert.AreEqual(message, exception.Message); - } - [Test] public void ThrowsIfWindowIndicatorWithAllBits() { WriteBytes(0x81, 0x7F); - TestThrows("unrecognized window indicator bits set"); + TestThrows("unrecognized window indicator bits set"); } [Test] public void ThrowsIfWindowIndicatorWithInvalidBit() { WriteBytes(0x08); - TestThrows("unrecognized window indicator bits set"); + TestThrows("unrecognized window indicator bits set"); } [Test] @@ -135,9 +119,11 @@ public void ThrowsExceptionIfCompressedActivate() [Test] public void TestValidWindowFields() { - WriteBytes(0x05, 0x10, 0x81, 0x00, 0x04, 0x00, 0x00, + WriteBytes(new byte[] { + 0x05, 0x10, 0x81, 0x00, 0x04, 0x00, 0x00, 0x04, 0x0, 0x02, 0x00, 0x00, 0x00, 0x01, - 0x0A, 0x0B, 0x0C, 0x0D, 0xCA, 0xFE); + 0x0A, 0x0B, 0x0C, 0x0D, 0xCA, 0xFE, + }); Assert.DoesNotThrow(() => decoder.Run()); Assert.AreEqual(patch.Length, patch.Position); @@ -153,9 +139,25 @@ public void TestValidWindowFields() Assert.AreEqual(0x02, window.Addresses.BaseStream.Length); Assert.AreEqual(0x01, window.Checksum); Assert.AreEqual(new byte[] { 0xA, 0xB, 0xC, 0xD }, window.Data.ReadBytes(4)); - //Assert.AreEqual(new byte[] { 0x0F }, window.Instructions.ReadBytes(1)); // No instruction to process Assert.AreEqual(new byte[] { 0xCA, 0xFE }, window.Addresses.ReadBytes(2)); } + + private void WriteGenericHeader() + { + WriteBytes(0xD6, 0xC3, 0xC4, 0x00, 0x00); + } + + private void WriteBytes(params byte[] data) + { + patch.Write(data, 0, data.Length); + patch.Position -= data.Length; + } + + private void TestThrows(string message) + where T : SystemException + { + T exception = Assert.Throws(() => decoder.Run()); + Assert.AreEqual(message, exception.Message); + } } } - diff --git a/src/Pleosoft.XdeltaSharp/AddressMode.cs b/src/Pleosoft.XdeltaSharp/AddressMode.cs index 7ee230d..a6bd866 100644 --- a/src/Pleosoft.XdeltaSharp/AddressMode.cs +++ b/src/Pleosoft.XdeltaSharp/AddressMode.cs @@ -17,14 +17,13 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -namespace Xdelta +namespace Pleosoft.XdeltaSharp { public enum AddressMode { Self, Here, Near, Same, - Invalid + Invalid, } } - diff --git a/src/Pleosoft.XdeltaSharp/Adler32.cs b/src/Pleosoft.XdeltaSharp/Adler32.cs index 0c04d22..3210b48 100644 --- a/src/Pleosoft.XdeltaSharp/Adler32.cs +++ b/src/Pleosoft.XdeltaSharp/Adler32.cs @@ -40,7 +40,7 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -namespace Xdelta +namespace Pleosoft.XdeltaSharp { using System; using System.Buffers; @@ -50,7 +50,7 @@ namespace Xdelta /// Adler-32 checksum algorithm. /// /// - /// For more information: https://en.wikipedia.org/wiki/Adler-32 + /// For more information: https://en.wikipedia.org/wiki/Adler-32. /// public static class Adler32 { diff --git a/src/Pleosoft.XdeltaSharp/Cache.cs b/src/Pleosoft.XdeltaSharp/Cache.cs index 3d1eb52..152097c 100644 --- a/src/Pleosoft.XdeltaSharp/Cache.cs +++ b/src/Pleosoft.XdeltaSharp/Cache.cs @@ -17,14 +17,14 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; - -namespace Xdelta +namespace Pleosoft.XdeltaSharp { + using System; + public class Cache { - private uint[] near; - private uint[] same; + private readonly uint[] near; + private readonly uint[] same; private int nextSlot; public Cache(byte nearSlots, byte sameSlots) @@ -70,7 +70,7 @@ public uint GetAddress(uint hereAddress, byte mode, VcdReader addressSection) case AddressMode.Same: int index = mode - (2 + NearSlots); - address = same[index * 256 + addressSection.ReadByte()]; + address = same[(index * 256) + addressSection.ReadByte()]; break; } @@ -105,4 +105,3 @@ private void Update(uint address) } } } - diff --git a/src/Pleosoft.XdeltaSharp/CodeTable.cs b/src/Pleosoft.XdeltaSharp/CodeTable.cs index f95e9ea..97b0b83 100644 --- a/src/Pleosoft.XdeltaSharp/CodeTable.cs +++ b/src/Pleosoft.XdeltaSharp/CodeTable.cs @@ -17,11 +17,11 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using Xdelta.Instructions; - -namespace Xdelta +namespace Pleosoft.XdeltaSharp { + using System; + using Pleosoft.XdeltaSharp.Vcdiff.Instructions; + public class CodeTable { private const int NumEntries = 256; @@ -33,19 +33,19 @@ public CodeTable(byte[] table, byte nearSlots, byte sameSlots) Initialize(table); } + public static CodeTable Default { + get { return new CodeTable(DefaultTable, DefaultNearSlots, DefaultSameSlots); } + } + public Cache Cache { get; private set; } - public static CodeTable Default { - get { return new CodeTable(DefaultTable, DefaultNearSlots, DefaultSameSlots); } - } - public void GetInstructions(int index, out Instruction inst1, out Instruction inst2) { inst1 = instructions[index * 2]; - inst2 = instructions[index * 2 + 1]; + inst2 = instructions[(index * 2) + 1]; } private void Initialize(byte[] table) @@ -53,11 +53,11 @@ private void Initialize(byte[] table) if (table.Length != NumEntries * 6) throw new FormatException("Invalid code table size"); - instructions = new Instruction[NumEntries*2]; + instructions = new Instruction[NumEntries * 2]; for (int i = 0, t = 0; i < NumEntries; i++) { // An entry of the table contains two instructions - instructions[i*2] = CreateInstruction(table[t++], table[t++], table[t++]); - instructions[i*2 + 1] = CreateInstruction(table[t++], table[t++], table[t++]); + instructions[i * 2] = CreateInstruction(table[t++], table[t++], table[t++]); + instructions[(i * 2) + 1] = CreateInstruction(table[t++], table[t++], table[t++]); } } @@ -80,6 +80,9 @@ private Instruction CreateInstruction(byte type, byte size, byte mode) return null; } +#pragma warning disable SA1201 // A field should not follow a method +#pragma warning disable SA1203 // Constant fields should appear before non-constant fields +#pragma warning disable SA1005 // Single line comment should begin with a space private const byte DefaultNearSlots = 4; private const byte DefaultSameSlots = 3; private static readonly byte[] DefaultTable = new byte[1536] { @@ -341,5 +344,8 @@ private Instruction CreateInstruction(byte type, byte size, byte mode) 03, 04, 07, 01, 01, 00, // 254 03, 04, 08, 01, 01, 00, // 255 }; +#pragma warning restore SA1201 // A field should not follow a method +#pragma warning restore SA1203 // Constant fields should appear before non-constant fields +#pragma warning restore SA1005 // Single line comment should begin with a space } -} \ No newline at end of file +} diff --git a/src/Pleosoft.XdeltaSharp/Decoder/Decoder.cs b/src/Pleosoft.XdeltaSharp/Decoder/Decoder.cs index 295c89c..11ca9dc 100644 --- a/src/Pleosoft.XdeltaSharp/Decoder/Decoder.cs +++ b/src/Pleosoft.XdeltaSharp/Decoder/Decoder.cs @@ -17,28 +17,28 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; - -namespace Xdelta +namespace Pleosoft.XdeltaSharp.Decoder { + using System; + using System.IO; + using Pleosoft.XdeltaSharp.Vcdiff; + public class Decoder : IDisposable { public Decoder(Stream input, Stream patch, Stream output) { - Input = input; - Patch = patch; + Input = input; + Patch = patch; Output = output; HeaderReader headerReader = new HeaderReader(); Header = headerReader.Read(patch); } - ~Decoder() - { - Dispose(false); - } - + public event ProgressChangedHandler ProgressChanged; + + public event FinishedHandler Finished; + public Stream Input { get; private set; @@ -64,9 +64,6 @@ public Decoder(Stream input, Stream patch, Stream output) private set; } - public event ProgressChangedHandler ProgressChanged; - public event FinishedHandler Finished; - public void Run() { WindowReader windowReader = new WindowReader(Patch, Header); @@ -92,13 +89,13 @@ public void Dispose() GC.SuppressFinalize(this); } - private void Dispose(bool disposing) + protected virtual void Dispose(bool disposing) { if (disposing) { DisposeLastWindow(); - Input = null; + Input = null; Output = null; - Patch = null; + Patch = null; Header = null; } } @@ -131,4 +128,4 @@ private void OnFinished() Finished(); } } -} \ No newline at end of file +} diff --git a/src/Pleosoft.XdeltaSharp/Decoder/HeaderReader.cs b/src/Pleosoft.XdeltaSharp/Decoder/HeaderReader.cs index 6d7d32d..2409809 100644 --- a/src/Pleosoft.XdeltaSharp/Decoder/HeaderReader.cs +++ b/src/Pleosoft.XdeltaSharp/Decoder/HeaderReader.cs @@ -17,11 +17,12 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; - -namespace Xdelta +namespace Pleosoft.XdeltaSharp.Decoder { + using System; + using System.IO; + using Pleosoft.XdeltaSharp.Vcdiff; + internal class HeaderReader { private const uint MagicStamp = 0xC4C3D6; @@ -58,7 +59,7 @@ private Header ReadHeader() Header header = new Header(); HeaderFields fields = (HeaderFields)vcdReader.ReadByte(); - if (fields.HasInvalidFlag(HeaderFields.Invalid)) + if ((fields & HeaderFields.All) != fields) throw new FormatException("unrecognized header indicator bits set"); header.SecondaryCompressor = SecondaryCompressor.None; @@ -80,4 +81,4 @@ private string ReadApplicationData() return Encoding.GetString(vcdReader.ReadBytes(length)); } } -} \ No newline at end of file +} diff --git a/src/Pleosoft.XdeltaSharp/Decoder/WindowDecoder.cs b/src/Pleosoft.XdeltaSharp/Decoder/WindowDecoder.cs index 0d58891..4a981c9 100644 --- a/src/Pleosoft.XdeltaSharp/Decoder/WindowDecoder.cs +++ b/src/Pleosoft.XdeltaSharp/Decoder/WindowDecoder.cs @@ -17,21 +17,22 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; -using Xdelta.Instructions; - -namespace Xdelta +namespace Pleosoft.XdeltaSharp.Decoder { + using System; + using System.IO; + using Pleosoft.XdeltaSharp.Vcdiff; + using Pleosoft.XdeltaSharp.Vcdiff.Instructions; + public class WindowDecoder { - private Stream input; - private Stream output; - private CodeTable codeTable; + private readonly Stream input; + private readonly Stream output; + private readonly CodeTable codeTable; public WindowDecoder(Stream input, Stream output) { - this.input = input; + this.input = input; this.output = output; this.codeTable = CodeTable.Default; } @@ -56,16 +57,15 @@ public void Decode(Window window) // Check all data has been decoded if (output.Position - window.TargetWindowOffset != window.TargetWindowLength) - throw new Exception("Target window not fully decoded"); + throw new InvalidOperationException("Target window not fully decoded"); // Check checksum if (window.Source.HasFlag(WindowFields.Adler32)) { output.Position = window.TargetWindowOffset; uint newAdler = Adler32.Run(output, window.TargetWindowLength); if (newAdler != window.Checksum) - throw new Exception("Invalid checksum"); + throw new InvalidOperationException("Invalid checksum"); } } } } - diff --git a/src/Pleosoft.XdeltaSharp/Decoder/WindowReader.cs b/src/Pleosoft.XdeltaSharp/Decoder/WindowReader.cs index 536e865..c920db1 100644 --- a/src/Pleosoft.XdeltaSharp/Decoder/WindowReader.cs +++ b/src/Pleosoft.XdeltaSharp/Decoder/WindowReader.cs @@ -17,17 +17,18 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; -using System.IO; - -namespace Xdelta +namespace Pleosoft.XdeltaSharp.Decoder { + using System; + using System.IO; + using Pleosoft.XdeltaSharp.Vcdiff; + internal class WindowReader { private const uint HardMaxWindowSize = 1u << 24; - private Header header; - private VcdReader vcdReader; + private readonly Header header; + private readonly VcdReader vcdReader; private uint lastWindowOffset; private uint lastWindowLength; @@ -61,7 +62,7 @@ private Window ReadWindow() // Get window indicator window.Source = (WindowFields)vcdReader.ReadByte(); - if (window.Source.HasInvalidFlag(WindowFields.Invalid)) + if ((window.Source & WindowFields.All) != window.Source) throw new FormatException("unrecognized window indicator bits set"); if (window.Source.HasFlag(WindowFields.Source) || window.Source.HasFlag(WindowFields.Target)) { @@ -96,7 +97,7 @@ private Window ReadWindow() // Get compressed / delta fields window.CompressedFields = (WindowCompressedFields)vcdReader.ReadByte(); - if (window.CompressedFields.HasInvalidFlag(WindowCompressedFields.Invalid)) + if ((window.CompressedFields & WindowCompressedFields.All) != window.CompressedFields) throw new FormatException("unrecognized delta indicator bits set"); // Compressed fields is only used with secondary compression @@ -105,9 +106,9 @@ private Window ReadWindow() throw new FormatException("invalid delta indicator bits set"); // Read section lengths - uint dataLength = vcdReader.ReadInteger(); + uint dataLength = vcdReader.ReadInteger(); uint instructionsLength = vcdReader.ReadInteger(); - uint addressesLength = vcdReader.ReadInteger(); + uint addressesLength = vcdReader.ReadInteger(); // Read checksum if so (it's in big-endian-non-integer) if (window.Source.HasFlag(WindowFields.Adler32)) { @@ -128,4 +129,3 @@ private Window ReadWindow() } } } - diff --git a/src/Pleosoft.XdeltaSharp/Pleosoft.XdeltaSharp.csproj b/src/Pleosoft.XdeltaSharp/Pleosoft.XdeltaSharp.csproj index f048f9a..489f4d7 100644 --- a/src/Pleosoft.XdeltaSharp/Pleosoft.XdeltaSharp.csproj +++ b/src/Pleosoft.XdeltaSharp/Pleosoft.XdeltaSharp.csproj @@ -3,6 +3,7 @@ Decompressor of VCDIFF patch files (RFC3284). netstandard2.0;net461 + Pleosoft.XdeltaSharp