-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop ancient zlib dependency in favor of native .NET library calls.
Due to different naming of those compression libraries per OS there is a custom dll import resolver used.
- Loading branch information
Showing
7 changed files
with
146 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace WowPacketParser.Misc | ||
{ | ||
public enum FlushCode | ||
{ | ||
NoFlush = 0, | ||
SyncFlush = 2, | ||
Finish = 4, | ||
Block = 5 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using WowPacketParser.Parsing.Parsers; | ||
|
||
namespace WowPacketParser.Misc | ||
{ | ||
internal partial class ZLibHelper | ||
{ | ||
private const string CompressionLib = "compression_native"; | ||
|
||
[LibraryImport(CompressionLib, EntryPoint = "CompressionNative_InflateInit2_")] | ||
private static partial int InflateInit2(ref ZStream zStream, int windowBits); | ||
|
||
[LibraryImport(CompressionLib, EntryPoint = "CompressionNative_Inflate")] | ||
private static partial int Inflate(ref ZStream zStream, FlushCode flush); | ||
|
||
[LibraryImport(CompressionLib, EntryPoint = "CompressionNative_InflateEnd")] | ||
private static partial int InflateEnd(ref ZStream zStream); | ||
|
||
public static void ResetInflateStream(int connectionIndex) | ||
{ | ||
ref var zStream = ref CollectionsMarshal.GetValueRefOrAddDefault(SessionHandler.ZStreams, connectionIndex, out bool exists); | ||
|
||
InflateInit2(ref zStream, 15); | ||
} | ||
|
||
public static bool TryInflate(int inflatedSize, int index, byte[] arr, ref byte[] newarr) | ||
{ | ||
try | ||
{ | ||
ref var zStream = ref CollectionsMarshal.GetValueRefOrAddDefault(SessionHandler.ZStreams, index, out bool exists); | ||
|
||
if (!exists) | ||
{ | ||
var initResult = InflateInit2(ref zStream, 15); | ||
|
||
if (initResult != 0) | ||
return false; | ||
} | ||
|
||
nint uncompressedDataPtr = Marshal.AllocHGlobal(inflatedSize); | ||
nint compressedDataPtr = Marshal.AllocHGlobal(arr.Length); | ||
|
||
Marshal.Copy(arr, 0, compressedDataPtr, arr.Length); | ||
|
||
zStream.AvailIn = (uint)arr.Length; | ||
zStream.NextIn = compressedDataPtr; | ||
zStream.AvailOut = (uint)inflatedSize; | ||
zStream.NextOut = uncompressedDataPtr; | ||
|
||
Inflate(ref zStream, FlushCode.SyncFlush); | ||
|
||
Marshal.Copy(uncompressedDataPtr, newarr, 0, inflatedSize); | ||
|
||
Marshal.FreeHGlobal(compressedDataPtr); | ||
Marshal.FreeHGlobal(uncompressedDataPtr); | ||
|
||
return true; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public static void Inflate(int inflatedSize, byte[] arr, byte[] newarr) | ||
{ | ||
ZStream zStream = default; | ||
|
||
InflateInit2(ref zStream, 15); | ||
|
||
nint uncompressedDataPtr = Marshal.AllocHGlobal(inflatedSize); | ||
nint compressedDataPtr = Marshal.AllocHGlobal(arr.Length); | ||
|
||
Marshal.Copy(arr, 0, compressedDataPtr, arr.Length); | ||
|
||
zStream.AvailIn = (uint)arr.Length; | ||
zStream.NextIn = compressedDataPtr; | ||
zStream.AvailOut = (uint)inflatedSize; | ||
zStream.NextOut = uncompressedDataPtr; | ||
|
||
Inflate(ref zStream, FlushCode.NoFlush); | ||
Inflate(ref zStream, FlushCode.Finish); | ||
InflateEnd(ref zStream); | ||
|
||
Marshal.Copy(uncompressedDataPtr, newarr, 0, inflatedSize); | ||
|
||
Marshal.FreeHGlobal(compressedDataPtr); | ||
Marshal.FreeHGlobal(uncompressedDataPtr); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace WowPacketParser.Misc | ||
{ | ||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | ||
public struct ZStream | ||
{ | ||
internal nint NextIn; | ||
internal nint NextOut; | ||
internal nint Msg; | ||
|
||
readonly nint InternalState; | ||
|
||
internal uint AvailIn; | ||
internal uint AvailOut; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters