2014-09-04 13:58:49 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2022-08-28 14:57:44 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2014-09-04 13:58:49 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Ionic.Zlib;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Protocol.Handlers
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Quick Zlib compression handling for network packet compression.
|
|
|
|
|
|
/// Note: Underlying compression handling is taken from the DotNetZip Library.
|
|
|
|
|
|
/// This library is open source and provided under the Microsoft Public License.
|
|
|
|
|
|
/// More info about DotNetZip at dotnetzip.codeplex.com.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class ZlibUtils
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Compress a byte array into another bytes array using Zlib compression
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="to_compress">Data to compress</param>
|
|
|
|
|
|
/// <returns>Compressed data as a byte array</returns>
|
|
|
|
|
|
public static byte[] Compress(byte[] to_compress)
|
|
|
|
|
|
{
|
2017-09-08 17:36:22 -07:00
|
|
|
|
byte[] data;
|
|
|
|
|
|
using (System.IO.MemoryStream memstream = new System.IO.MemoryStream())
|
2014-09-04 13:58:49 +02:00
|
|
|
|
{
|
2017-09-08 17:36:22 -07:00
|
|
|
|
using (ZlibStream stream = new ZlibStream(memstream, CompressionMode.Compress))
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Write(to_compress, 0, to_compress.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
data = memstream.ToArray();
|
2014-09-04 13:58:49 +02:00
|
|
|
|
}
|
2017-09-08 17:36:22 -07:00
|
|
|
|
return data;
|
2014-09-04 13:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Decompress a byte array into another byte array of the specified size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="to_decompress">Data to decompress</param>
|
|
|
|
|
|
/// <param name="size_uncompressed">Size of the data once decompressed</param>
|
|
|
|
|
|
/// <returns>Decompressed data as a byte array</returns>
|
|
|
|
|
|
public static byte[] Decompress(byte[] to_decompress, int size_uncompressed)
|
|
|
|
|
|
{
|
|
|
|
|
|
ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_decompress, false), CompressionMode.Decompress);
|
|
|
|
|
|
byte[] packetData_decompressed = new byte[size_uncompressed];
|
|
|
|
|
|
stream.Read(packetData_decompressed, 0, size_uncompressed);
|
|
|
|
|
|
stream.Close();
|
|
|
|
|
|
return packetData_decompressed;
|
|
|
|
|
|
}
|
2016-08-21 15:44:15 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Decompress a byte array into another byte array of a potentially unlimited size (!)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="to_decompress">Data to decompress</param>
|
|
|
|
|
|
/// <returns>Decompressed data as byte array</returns>
|
|
|
|
|
|
public static byte[] Decompress(byte[] to_decompress)
|
|
|
|
|
|
{
|
|
|
|
|
|
ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_decompress, false), CompressionMode.Decompress);
|
|
|
|
|
|
byte[] buffer = new byte[16 * 1024];
|
|
|
|
|
|
using (System.IO.MemoryStream decompressedBuffer = new System.IO.MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
int read;
|
|
|
|
|
|
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
|
|
|
decompressedBuffer.Write(buffer, 0, read);
|
|
|
|
|
|
return decompressedBuffer.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-09-04 13:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|