Skip to content

Commit

Permalink
Added memory zlib helper and changed library namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
AraHaan committed Dec 8, 2018
1 parent 3d7a69a commit 353326a
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 19 deletions.
4 changes: 1 addition & 3 deletions Adler32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Els_kom_Core", AllInternalsVisible = true)]

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
internal sealed class Adler32
{
Expand Down
2 changes: 1 addition & 1 deletion Deflate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System;

Expand Down
2 changes: 1 addition & 1 deletion InfBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System;

Expand Down
2 changes: 1 addition & 1 deletion InfCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System;

Expand Down
2 changes: 1 addition & 1 deletion InfTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System;

Expand Down
2 changes: 1 addition & 1 deletion Inflate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
internal sealed class Inflate
{
Expand Down
118 changes: 118 additions & 0 deletions MemoryZlib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) 2014-2018, Els_kom org.
// https://github.com/Elskom/
// All rights reserved.
// license: MIT, see LICENSE for more details.

namespace Elskom.Generic.Libs
{
using System.IO;

/// <summary>
/// Zlib Memory Compression and Decompression Helper Class.
/// </summary>
public static class MemoryZlib
{
/// <summary>
/// Compresses data using the default compression level.
/// </summary>
/// <param name="inData">The original input data.</param>
/// <param name="outData">The compressed output data.</param>
/// <param name="adler32">The output adler32 of the data.</param>
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception>
public static void CompressData(byte[] inData, out byte[] outData, out int adler32)
=> CompressData(inData, out outData, ZlibConst.ZDEFAULTCOMPRESSION, out adler32);

/// <summary>
/// Compresses data using the default compression level.
/// </summary>
/// <param name="inData">The original input data.</param>
/// <param name="outData">The compressed output data.</param>
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception>
public static void CompressData(byte[] inData, out byte[] outData)
=> CompressData(inData, out outData, ZlibConst.ZDEFAULTCOMPRESSION);

/// <summary>
/// Compresses data using an specific compression level.
/// </summary>
/// <param name="inData">The original input data.</param>
/// <param name="outData">The compressed output data.</param>
/// <param name="level">The compression level to use.</param>
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception>
// discard returned adler32. The caller does not want it.
public static void CompressData(byte[] inData, out byte[] outData, int level)
=> CompressData(inData, out outData, level, out var adler32);

/// <summary>
/// Compresses data using an specific compression level.
/// </summary>
/// <param name="inData">The original input data.</param>
/// <param name="outData">The compressed output data.</param>
/// <param name="level">The compression level to use.</param>
/// <param name="adler32">The output adler32 of the data.</param>
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception>
public static void CompressData(byte[] inData, out byte[] outData, int level, out int adler32)
{
using (var outMemoryStream = new MemoryStream())
using (var outZStream = new ZOutputStream(outMemoryStream, level))
using (Stream inMemoryStream = new MemoryStream(inData))
{
try
{
inMemoryStream.CopyTo(outZStream);
}
catch (ZStreamException)
{
// the compression or decompression failed.
}

outZStream.Flush();
try
{
outZStream.Finish();
}
catch (ZStreamException ex)
{
throw new NotPackableException("Compression Failed.", ex);
}

outData = outMemoryStream.ToArray();
adler32 = (int)(outZStream.Z.Adler & 0xffff);
}
}

/// <summary>
/// Decompresses data.
/// </summary>
/// <param name="inData">The compressed input data.</param>
/// <param name="outData">The decompressed output data.</param>
/// <exception cref="NotUnpackableException">Thrown when the stream Errors in any way.</exception>
public static void DecompressData(byte[] inData, out byte[] outData)
{
using (var outMemoryStream = new MemoryStream())
using (var outZStream = new ZOutputStream(outMemoryStream))
using (Stream inMemoryStream = new MemoryStream(inData))
{
try
{
inMemoryStream.CopyTo(outZStream);
}
catch (ZStreamException)
{
// the compression or decompression failed.
}

outZStream.Flush();
try
{
outZStream.Finish();
}
catch (ZStreamException ex)
{
throw new NotUnpackableException("Decompression Failed.", ex);
}

outData = outMemoryStream.ToArray();
}
}
}
}
44 changes: 44 additions & 0 deletions NotPackableException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2014-2018, Els_kom org.
// https://github.com/Elskom/
// All rights reserved.
// license: MIT, see LICENSE for more details.

namespace Elskom.Generic.Libs
{
using System;
using System.IO;

/// <summary>
/// Zlib Memory Packing failure error.
/// </summary>
[Serializable]
public sealed class NotPackableException : IOException
{
/// <summary>
/// Initializes a new instance of the <see cref="NotPackableException"/> class.
/// </summary>
public NotPackableException()
: base()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NotPackableException"/> class.
/// </summary>
/// <param name="s">The error string.</param>
public NotPackableException(string s)
: base(s)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NotPackableException"/> class.
/// </summary>
/// <param name="s">The error string.</param>
/// <param name="ex">The Exception that caused this Exception.</param>
public NotPackableException(string s, Exception ex)
: base(s, ex)
{
}
}
}
45 changes: 45 additions & 0 deletions NotUnpackableException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2014-2018, Els_kom org.
// https://github.com/Elskom/
// All rights reserved.
// license: MIT, see LICENSE for more details.

namespace Elskom.Generic.Libs
{
using System;
using System.IO;

/// <summary>
/// Zlib Memory Unpacking failure error.
/// </summary>
[Serializable]
public sealed class NotUnpackableException : IOException
{
/// <summary>
/// Initializes a new instance of the <see cref="NotUnpackableException"/> class with no argrument.
/// </summary>
public NotUnpackableException()
: base()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NotUnpackableException"/> class with an string argrument.
/// </summary>
/// <param name="s">The error string.</param>
public NotUnpackableException(string s)
: base(s)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NotUnpackableException"/> class with an string argrument
/// and the exception that cuased this exception.
/// </summary>
/// <param name="s">The error string.</param>
/// <param name="ex">The Exception that caused this Exception.</param>
public NotUnpackableException(string s, Exception ex)
: base(s, ex)
{
}
}
}
2 changes: 1 addition & 1 deletion StaticTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
internal sealed class StaticTree
{
Expand Down
2 changes: 1 addition & 1 deletion SupportClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System.IO;
using System.Text;
Expand Down
2 changes: 1 addition & 1 deletion Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
internal sealed class Tree
{
Expand Down
2 changes: 1 addition & 1 deletion ZInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System.IO;

Expand Down
2 changes: 1 addition & 1 deletion ZOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System;
using System.IO;
Expand Down
2 changes: 1 addition & 1 deletion ZStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System;

Expand Down
2 changes: 1 addition & 1 deletion ZStreamException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
using System.IO;

Expand Down
2 changes: 1 addition & 1 deletion ZlibConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All rights reserved.
// license: see LICENSE for more details.

namespace Els_kom.Compression.Libs.Zlib
namespace Elskom.Generic.Libs
{
/// <summary>
/// Class that holds the contant values to zlib.
Expand Down
6 changes: 3 additions & 3 deletions zlib.managed.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1;netstandard2.0;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
Expand All @@ -13,8 +13,8 @@
<PackageLicenseUrl>https://github.com/Elskom/zlib.managed/blob/master/LICENSE</PackageLicenseUrl>
<Copyright>Copyright (c) 2018</Copyright>
<Description>A cleaned up and bug fixed version of zlib.net. This is also planned to be updated frequently if needed too.</Description>
<PackageReleaseNotes>Target .NET Standard and multiple .NET Framework versions.</PackageReleaseNotes>
<Version>1.0.2</Version>
<PackageReleaseNotes>Added memory zlib helper class and rename library namespace.</PackageReleaseNotes>
<Version>1.1.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down

0 comments on commit 353326a

Please sign in to comment.