Remove unnecessary dependencies

This commit is contained in:
Afshin Arani 2020-04-05 19:33:18 +08:00 committed by Andres G. Aragoneses
parent 709e5be7cc
commit 6d2c77f5ef
11 changed files with 87 additions and 126 deletions

View file

@ -15,7 +15,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel
# Table of contents # Table of contents
- [How do I add this to my project?](#how-do-i-add-this-to-my-project) - [How do I add this to my project?](#how-do-i-add-this-to-my-project)
- [Dependencies](#dependencies)
- [Starter Guide](#starter-guide) - [Starter Guide](#starter-guide)
- [Quick configuration](#quick-configuration) - [Quick configuration](#quick-configuration)
- [First requests](#first-requests) - [First requests](#first-requests)
@ -41,11 +40,6 @@ or build from source
1. Compile source with VS2015 or MonoDevelop 1. Compile source with VS2015 or MonoDevelop
1. Add reference to ```TLSharp.Core.dll``` to your awesome project. 1. Add reference to ```TLSharp.Core.dll``` to your awesome project.
# Dependencies
TLSharp has a few dependenices, most of functionality implemented from scratch.
All dependencies listed in [package.conf file](https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/packages.config).
# Starter Guide # Starter Guide
## Quick Configuration ## Quick Configuration

View file

@ -4,32 +4,42 @@ using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ionic.Crc;
namespace TLSharp.Core.MTProto.Crypto namespace TLSharp.Core.MTProto.Crypto
{ {
public class Crc32 : HashAlgorithm /// <summary>
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
/// </summary>
/// <remarks>
/// Crc32 should only be used for backward compatibility with older file formats
/// and algorithms. It is not secure enough for new applications.
/// If you need to call multiple times for the same data either use the HashAlgorithm
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
/// being passed in as the seed for the next Compute call.
/// </remarks>
public sealed class Crc32 : HashAlgorithm
{ {
public const UInt32 DefaultPolynomial = 0xedb88320u; public const UInt32 DefaultPolynomial = 0xedb88320u;
public const UInt32 DefaultSeed = 0xffffffffu; public const UInt32 DefaultSeed = 0xffffffffu;
private UInt32 hash; static UInt32[] defaultTable;
private UInt32 seed;
private UInt32[] table; readonly UInt32 seed;
private static UInt32[] defaultTable; readonly UInt32[] table;
UInt32 hash;
public Crc32() public Crc32()
: this(DefaultPolynomial, DefaultSeed)
{ {
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
hash = seed;
} }
public Crc32(UInt32 polynomial, UInt32 seed) public Crc32(UInt32 polynomial, UInt32 seed)
{ {
if (!BitConverter.IsLittleEndian)
throw new PlatformNotSupportedException("Not supported on Big Endian processors");
table = InitializeTable(polynomial); table = InitializeTable(polynomial);
this.seed = seed; this.seed = hash = seed;
hash = seed;
} }
public override void Initialize() public override void Initialize()
@ -37,35 +47,28 @@ namespace TLSharp.Core.MTProto.Crypto
hash = seed; hash = seed;
} }
protected override void HashCore(byte[] buffer, int start, int length) protected override void HashCore(byte[] array, int ibStart, int cbSize)
{ {
hash = CalculateHash(table, hash, buffer, start, length); hash = CalculateHash(table, hash, array, ibStart, cbSize);
} }
/// <summary>
/// Возвращает хеш в BigEndian
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal() protected override byte[] HashFinal()
{ {
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash); var hashBuffer = UInt32ToBigEndianBytes(~hash);
this.HashValue = hashBuffer; HashValue = hashBuffer;
return hashBuffer; return hashBuffer;
} }
public override int HashSize public override int HashSize { get { return 32; } }
{
get { return 32; }
}
public static UInt32 Compute(byte[] buffer) public static UInt32 Compute(byte[] buffer)
{ {
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length); return Compute(DefaultSeed, buffer);
} }
public static UInt32 Compute(UInt32 seed, byte[] buffer) public static UInt32 Compute(UInt32 seed, byte[] buffer)
{ {
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length); return Compute(DefaultPolynomial, seed, buffer);
} }
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
@ -73,20 +76,20 @@ namespace TLSharp.Core.MTProto.Crypto
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
} }
private static UInt32[] InitializeTable(UInt32 polynomial) static UInt32[] InitializeTable(UInt32 polynomial)
{ {
if (polynomial == DefaultPolynomial && defaultTable != null) if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable; return defaultTable;
UInt32[] createTable = new UInt32[256]; var createTable = new UInt32[256];
for (int i = 0; i < 256; i++) for (var i = 0; i < 256; i++)
{ {
UInt32 entry = (UInt32)i; var entry = (UInt32)i;
for (int j = 0; j < 8; j++) for (var j = 0; j < 8; j++)
if ((entry & 1) == 1) if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
createTable[i] = entry; createTable[i] = entry;
} }
@ -96,25 +99,23 @@ namespace TLSharp.Core.MTProto.Crypto
return createTable; return createTable;
} }
private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size) static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
{ {
UInt32 crc = seed; var hash = seed;
for (int i = start; i < size; i++) for (var i = start; i < start + size; i++)
unchecked hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
{ return hash;
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
return crc;
} }
private byte[] UInt32ToBigEndianBytes(UInt32 x) static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
{ {
return new byte[] { var result = BitConverter.GetBytes(uint32);
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff), if (BitConverter.IsLittleEndian)
(byte)((x >> 8) & 0xff), Array.Reverse(result);
(byte)(x & 0xff)
}; return result;
} }
} }
} }

View file

@ -2,11 +2,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ionic.Zlib;
using TeleSharp.TL; using TeleSharp.TL;
using TLSharp.Core.Exceptions; using TLSharp.Core.Exceptions;
using TLSharp.Core.MTProto; using TLSharp.Core.MTProto;
@ -251,11 +251,20 @@ namespace TLSharp.Core.Network
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
uint code = messageReader.ReadUInt32(); uint code = messageReader.ReadUInt32();
byte[] packedData = GZipStream.UncompressBuffer(Serializers.Bytes.Read(messageReader));
using (MemoryStream packedStream = new MemoryStream(packedData, false)) byte[] packedData = Serializers.Bytes.Read(messageReader);
using (BinaryReader compressedReader = new BinaryReader(packedStream)) using (var ms = new MemoryStream())
{ {
processMessage(messageId, sequence, compressedReader, request, token); using (var packedStream = new MemoryStream(packedData, false))
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
{
zipStream.CopyTo(ms);
ms.Position = 0;
}
using (BinaryReader compressedReader = new BinaryReader(ms))
{
processMessage(messageId, sequence, compressedReader, request, token);
}
} }
return true; return true;
@ -337,27 +346,20 @@ namespace TLSharp.Core.Network
} }
else if (innerCode == 0x3072cfa1) else if (innerCode == 0x3072cfa1)
{ {
try // gzip_packed
byte[] packedData = Serializers.Bytes.Read(messageReader);
using (var ms = new MemoryStream())
{ {
// gzip_packed using (var packedStream = new MemoryStream(packedData, false))
byte[] packedData = Serializers.Bytes.Read(messageReader); using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
using (var ms = new MemoryStream())
{ {
using (var packedStream = new MemoryStream(packedData, false)) zipStream.CopyTo(ms);
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) ms.Position = 0;
{ }
zipStream.CopyTo(ms); using (var compressedReader = new BinaryReader(ms))
ms.Position = 0; {
} request.DeserializeResponse(compressedReader);
using (var compressedReader = new BinaryReader(ms))
{
request.DeserializeResponse(compressedReader);
}
} }
}
catch (ZlibException ex)
{
} }
} }
else else

View file

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using Ionic.Crc; using System.Linq;
using TLSharp.Core.MTProto.Crypto;
namespace TLSharp.Core.Network namespace TLSharp.Core.Network
{ {
@ -35,9 +36,9 @@ namespace TLSharp.Core.Network
binaryWriter.Write(Body.Length + 12); binaryWriter.Write(Body.Length + 12);
binaryWriter.Write(SequneceNumber); binaryWriter.Write(SequneceNumber);
binaryWriter.Write(Body); binaryWriter.Write(Body);
var crc32 = new CRC32(); var crc32 = new Crc32();
crc32.SlurpBlock(memoryStream.GetBuffer(), 0, 8 + Body.Length); var checksum = crc32.ComputeHash(memoryStream.GetBuffer(), 0, 8 + Body.Length).Reverse().ToArray();
binaryWriter.Write(crc32.Crc32Result); binaryWriter.Write(checksum);
var transportPacket = memoryStream.ToArray(); var transportPacket = memoryStream.ToArray();
@ -67,13 +68,12 @@ namespace TLSharp.Core.Network
var seq = binaryReader.ReadInt32(); var seq = binaryReader.ReadInt32();
byte[] packet = binaryReader.ReadBytes(packetLength - 12); byte[] packet = binaryReader.ReadBytes(packetLength - 12);
var checksum = (int)binaryReader.ReadInt32(); var checksum = binaryReader.ReadBytes(4);
var crc32 = new CRC32(); var crc32 = new Crc32();
crc32.SlurpBlock(body, 0, packetLength - 4); var computedChecksum = crc32.ComputeHash(body, 0, packetLength - 4).Reverse();
var validChecksum = crc32.Crc32Result;
if (checksum != validChecksum) if (!checksum.SequenceEqual(computedChecksum))
{ {
throw new InvalidOperationException("invalid checksum! skip"); throw new InvalidOperationException("invalid checksum! skip");
} }

View file

@ -1,8 +1,10 @@
using System; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using TLSharp.Core.MTProto.Crypto;
namespace TLSharp.Core.Network namespace TLSharp.Core.Network
{ {
@ -73,18 +75,16 @@ namespace TLSharp.Core.Network
var crcBytes = new byte[4]; var crcBytes = new byte[4];
if (await stream.ReadAsync(crcBytes, 0, 4, token).ConfigureAwait(false) != 4) if (await stream.ReadAsync(crcBytes, 0, 4, token).ConfigureAwait(false) != 4)
throw new InvalidOperationException("Couldn't read the crc"); throw new InvalidOperationException("Couldn't read the crc");
int checksum = BitConverter.ToInt32(crcBytes, 0);
byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length]; byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];
Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length); Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length); Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length); Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
var crc32 = new Ionic.Crc.CRC32(); var crc32 = new Crc32();
crc32.SlurpBlock(rv, 0, rv.Length); var computedChecksum = crc32.ComputeHash(rv).Reverse();
var validChecksum = crc32.Crc32Result;
if (checksum != validChecksum) if (!crcBytes.SequenceEqual(computedChecksum))
{ {
throw new InvalidOperationException("invalid checksum! skip"); throw new InvalidOperationException("invalid checksum! skip");
} }

View file

@ -31,10 +31,6 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Ionic.ZLib, Version=2.0.0.14, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MarkerMetro.Unity.Ionic.Zlib.2.0.0.14\lib\net35\Ionic.ZLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />

View file

@ -20,8 +20,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel
<copyright>Copyright 2016</copyright> <copyright>Copyright 2016</copyright>
</metadata> </metadata>
<files> <files>
<file src="bin\Debug\BigMath.dll" target="lib\net45\BigMath.dll" />
<file src="bin\Debug\Ionic.ZLib.dll" target="lib\net45\Ionic.ZLib.dll" />
<file src="bin\Debug\TeleSharp.TL.dll" target="lib\net45\TeleSharp.TL.dll" /> <file src="bin\Debug\TeleSharp.TL.dll" target="lib\net45\TeleSharp.TL.dll" />
</files> </files>
</package> </package>

View file

@ -1,5 +1,3 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="DotNetZip" version="1.11.0" targetFramework="net451" />
<package id="MarkerMetro.Unity.Ionic.Zlib" version="2.0.0.14" targetFramework="net452" />
</packages> </packages>

View file

@ -1,5 +1,4 @@
using BigMath; 
using BigMath.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -159,26 +158,4 @@ namespace TeleSharp.TL
writer.Write(src); writer.Write(src);
} }
} }
public class Int128Util
{
public static Int128 Deserialize(BinaryReader reader)
{
return reader.ReadBytes(16).ToInt128(0, true);
}
public static void Serialize(Int128 src, BinaryWriter writer)
{
writer.Write(src.ToBytes(true));
}
}
public class Int256Util
{
public static Int256 Deserialize(BinaryReader reader)
{
return reader.ReadBytes(32).ToInt256(0, true);
}
public static void Serialize(Int256 src, BinaryWriter writer)
{
writer.Write(src.ToBytes(true));
}
}
} }

View file

@ -31,10 +31,6 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="BigMath, Version=0.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\BigMath.0.5.0\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\BigMath.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />

View file

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="BigMath" version="0.5.0" targetFramework="net452" />
</packages> </packages>