Skip to content

Commit 9ecaa8a

Browse files
authored
Add packet deflate/inflate with zlib. (jo3bingham#2)
* Add zlib library from ComponentAce We can't use the zlib library in BouncyCastle, or add the nuget package of another, because we need to make edits to the source in order to decompress packets properly. * Modification to properly inflate Tibia packets * Check if packet is compressed and inflate it
1 parent 174ce48 commit 9ecaa8a

15 files changed

Lines changed: 5685 additions & 2 deletions

TibiaAPI/Network/Connection.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010

11+
using ComponentAce.Compression.Libs.zlib;
1112
using Newtonsoft.Json;
1213

1314
namespace OXGaming.TibiaAPI.Network
@@ -31,6 +32,8 @@ public class Connection : IDisposable
3132

3233
private readonly Rsa _rsa = new Rsa();
3334

35+
private ZStream _zStream = new ZStream();
36+
3437
private LoginData _loginData;
3538

3639
private Socket _clientSocket;
@@ -88,6 +91,8 @@ public bool Start()
8891

8992
try
9093
{
94+
_zStream.inflateInit(-15);
95+
9196
_httpListener.Start();
9297
_httpListener.BeginGetContext(new AsyncCallback(BeginGetContextCallback), _httpListener);
9398

@@ -118,6 +123,7 @@ public void Stop()
118123

119124
try
120125
{
126+
_zStream.inflateEnd();
121127
_httpListener.Close();
122128
_tcpListener.Stop();
123129
_clientSocket.Close();
@@ -224,6 +230,9 @@ private void ResetConnection()
224230
{
225231
_serverSocket.Close();
226232
}
233+
234+
_zStream.inflateEnd();
235+
_zStream.inflateInit(-15);
227236
}
228237

229238
/// <summary>
@@ -639,11 +648,34 @@ private void BeginReceiveServerCallback(IAsyncResult ar)
639648
{
640649
if (!_xtea.Decrypt(_serverMessage))
641650
{
642-
Console.WriteLine("BeginReceiveServerCallback: Failed to decrypt!");
651+
throw new Exception("[Connection.BeginReceiveServerCallback] XTEA decryption failed.");
643652
}
653+
654+
if ((BitConverter.ToUInt32(_serverMessage.GetBuffer(), 2) & 0xC0000000) != 0)
655+
{
656+
var compressedSize = BitConverter.ToUInt16(_serverMessage.GetBuffer(), 6);
657+
var inBuffer = new byte[compressedSize];
658+
var outBuffer = new byte[NetworkMessage.MaxMessageSize];
659+
660+
Array.Copy(_serverMessage.GetBuffer(), 8, inBuffer, 0, compressedSize);
661+
662+
_zStream.next_in = inBuffer;
663+
_zStream.next_in_index = 0;
664+
_zStream.avail_in = inBuffer.Length;
665+
_zStream.next_out = outBuffer;
666+
_zStream.next_out_index = 0;
667+
_zStream.avail_out = outBuffer.Length;
668+
669+
var ret = _zStream.inflate(2);
670+
if (ret != zlibConst.Z_OK)
671+
{
672+
throw new Exception($"[Connection.BeginReceiveServerCallback] zlib inflation failed: {ret}");
673+
}
674+
}
675+
644676
if (!_xtea.Encrypt(_serverMessage))
645677
{
646-
Console.WriteLine("BeginReceiveServerCallback: Failed to encrypt!");
678+
throw new Exception("[Connection.BeginReceiveServerCallback] XTEA encryption failed.");
647679
}
648680
}
649681
}

TibiaAPI/Utilities/Zlib/Adler32.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// This file isn't generated, but this comment is necessary to exclude it from StyleCop analysis.
2+
// <auto-generated/>
3+
4+
// Copyright (c) 2006, ComponentAce
5+
// http://www.componentace.com
6+
// All rights reserved.
7+
8+
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9+
10+
// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11+
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12+
// Neither the name of ComponentAce nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
15+
16+
17+
/*
18+
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
19+
20+
Redistribution and use in source and binary forms, with or without
21+
modification, are permitted provided that the following conditions are met:
22+
23+
1. Redistributions of source code must retain the above copyright notice,
24+
this list of conditions and the following disclaimer.
25+
26+
2. Redistributions in binary form must reproduce the above copyright
27+
notice, this list of conditions and the following disclaimer in
28+
the documentation and/or other materials provided with the distribution.
29+
30+
3. The names of the authors may not be used to endorse or promote products
31+
derived from this software without specific prior written permission.
32+
33+
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
34+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
36+
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
37+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39+
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43+
*/
44+
/*
45+
* This program is based on zlib-1.1.3, so all credit should go authors
46+
* Jean-loup Gailly([email protected]) and Mark Adler([email protected])
47+
* and contributors of zlib.
48+
*/
49+
namespace ComponentAce.Compression.Libs.zlib
50+
{
51+
52+
sealed class Adler32
53+
{
54+
55+
// largest prime smaller than 65536
56+
private const int BASE = 65521;
57+
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
58+
private const int NMAX = 5552;
59+
60+
internal long adler32(long adler, byte[] buf, int index, int len)
61+
{
62+
if (buf == null)
63+
{
64+
return 1L;
65+
}
66+
67+
long s1 = adler & 0xffff;
68+
long s2 = (adler >> 16) & 0xffff;
69+
int k;
70+
71+
while (len > 0)
72+
{
73+
k = len < NMAX ? len : NMAX;
74+
len -= k;
75+
while (k >= 16)
76+
{
77+
s1 += (buf[index++] & 0xff); s2 += s1;
78+
s1 += (buf[index++] & 0xff); s2 += s1;
79+
s1 += (buf[index++] & 0xff); s2 += s1;
80+
s1 += (buf[index++] & 0xff); s2 += s1;
81+
s1 += (buf[index++] & 0xff); s2 += s1;
82+
s1 += (buf[index++] & 0xff); s2 += s1;
83+
s1 += (buf[index++] & 0xff); s2 += s1;
84+
s1 += (buf[index++] & 0xff); s2 += s1;
85+
s1 += (buf[index++] & 0xff); s2 += s1;
86+
s1 += (buf[index++] & 0xff); s2 += s1;
87+
s1 += (buf[index++] & 0xff); s2 += s1;
88+
s1 += (buf[index++] & 0xff); s2 += s1;
89+
s1 += (buf[index++] & 0xff); s2 += s1;
90+
s1 += (buf[index++] & 0xff); s2 += s1;
91+
s1 += (buf[index++] & 0xff); s2 += s1;
92+
s1 += (buf[index++] & 0xff); s2 += s1;
93+
k -= 16;
94+
}
95+
if (k != 0)
96+
{
97+
do
98+
{
99+
s1 += (buf[index++] & 0xff); s2 += s1;
100+
}
101+
while (--k != 0);
102+
}
103+
s1 %= BASE;
104+
s2 %= BASE;
105+
}
106+
return (s2 << 16) | s1;
107+
}
108+
109+
}
110+
}

0 commit comments

Comments
 (0)