Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Remove unnecessary dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Afshin Arani authored and knocte committed Apr 5, 2020
1 parent 709e5be commit 6d2c77f
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 126 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel
# Table of contents

- [How do I add this to my project?](#how-do-i-add-this-to-my-project)
- [Dependencies](#dependencies)
- [Starter Guide](#starter-guide)
- [Quick configuration](#quick-configuration)
- [First requests](#first-requests)
Expand All @@ -41,11 +40,6 @@ or build from source
1. Compile source with VS2015 or MonoDevelop
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

## Quick Configuration
Expand Down
93 changes: 47 additions & 46 deletions TLSharp.Core/MTProto/Crypto/Crc32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,92 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Ionic.Crc;

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 DefaultSeed = 0xffffffffu;

private UInt32 hash;
private UInt32 seed;
private UInt32[] table;
private static UInt32[] defaultTable;
static UInt32[] defaultTable;

readonly UInt32 seed;
readonly UInt32[] table;
UInt32 hash;

public Crc32()
: this(DefaultPolynomial, DefaultSeed)
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
hash = seed;
}

public Crc32(UInt32 polynomial, UInt32 seed)
{
if (!BitConverter.IsLittleEndian)
throw new PlatformNotSupportedException("Not supported on Big Endian processors");

table = InitializeTable(polynomial);
this.seed = seed;
hash = seed;
this.seed = hash = seed;
}

public override void Initialize()
{
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()
{
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
this.HashValue = hashBuffer;
var hashBuffer = UInt32ToBigEndianBytes(~hash);
HashValue = hashBuffer;
return hashBuffer;
}

public override int HashSize
{
get { return 32; }
}
public override int HashSize { get { return 32; } }

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)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
return Compute(DefaultPolynomial, seed, buffer);
}

public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
{
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)
return defaultTable;

UInt32[] createTable = new UInt32[256];
for (int i = 0; i < 256; i++)
var createTable = new UInt32[256];
for (var i = 0; i < 256; i++)
{
UInt32 entry = (UInt32)i;
for (int j = 0; j < 8; j++)
var entry = (UInt32)i;
for (var j = 0; j < 8; j++)
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
entry >>= 1;
createTable[i] = entry;
}

Expand All @@ -96,25 +99,23 @@ private static UInt32[] InitializeTable(UInt32 polynomial)
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;
for (int i = start; i < size; i++)
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
return crc;
var hash = seed;
for (var i = start; i < start + size; i++)
hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
return hash;
}

private byte[] UInt32ToBigEndianBytes(UInt32 x)
static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
{
return new byte[] {
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff),
(byte)((x >> 8) & 0xff),
(byte)(x & 0xff)
};
var result = BitConverter.GetBytes(uint32);

if (BitConverter.IsLittleEndian)
Array.Reverse(result);

return result;
}
}

}
48 changes: 25 additions & 23 deletions TLSharp.Core/Network/MtProtoSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Ionic.Zlib;
using TeleSharp.TL;
using TLSharp.Core.Exceptions;
using TLSharp.Core.MTProto;
Expand Down Expand Up @@ -251,11 +251,20 @@ private bool HandleUpdate(ulong messageId, int sequence, BinaryReader messageRea
token.ThrowIfCancellationRequested();

uint code = messageReader.ReadUInt32();
byte[] packedData = GZipStream.UncompressBuffer(Serializers.Bytes.Read(messageReader));
using (MemoryStream packedStream = new MemoryStream(packedData, false))
using (BinaryReader compressedReader = new BinaryReader(packedStream))

byte[] packedData = Serializers.Bytes.Read(messageReader);
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;
Expand Down Expand Up @@ -337,27 +346,20 @@ private bool HandleRpcResult(ulong messageId, int sequence, BinaryReader message
}
else if (innerCode == 0x3072cfa1)
{
try
// gzip_packed
byte[] packedData = Serializers.Bytes.Read(messageReader);
using (var ms = new MemoryStream())
{
// gzip_packed
byte[] packedData = Serializers.Bytes.Read(messageReader);
using (var ms = new MemoryStream())
using (var packedStream = new MemoryStream(packedData, false))
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
{
using (var packedStream = new MemoryStream(packedData, false))
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
{
zipStream.CopyTo(ms);
ms.Position = 0;
}
using (var compressedReader = new BinaryReader(ms))
{
request.DeserializeResponse(compressedReader);
}
zipStream.CopyTo(ms);
ms.Position = 0;
}
using (var compressedReader = new BinaryReader(ms))
{
request.DeserializeResponse(compressedReader);
}
}
catch (ZlibException ex)
{

}
}
else
Expand Down
18 changes: 9 additions & 9 deletions TLSharp.Core/Network/TcpMessage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Ionic.Crc;
using System.Linq;
using TLSharp.Core.MTProto.Crypto;

namespace TLSharp.Core.Network
{
Expand Down Expand Up @@ -35,9 +36,9 @@ and 4 CRC32 bytes at the end (length, sequence number, and payload together).
binaryWriter.Write(Body.Length + 12);
binaryWriter.Write(SequneceNumber);
binaryWriter.Write(Body);
var crc32 = new CRC32();
crc32.SlurpBlock(memoryStream.GetBuffer(), 0, 8 + Body.Length);
binaryWriter.Write(crc32.Crc32Result);
var crc32 = new Crc32();
var checksum = crc32.ComputeHash(memoryStream.GetBuffer(), 0, 8 + Body.Length).Reverse().ToArray();
binaryWriter.Write(checksum);

var transportPacket = memoryStream.ToArray();

Expand Down Expand Up @@ -67,13 +68,12 @@ public static TcpMessage Decode(byte[] body)

var seq = binaryReader.ReadInt32();
byte[] packet = binaryReader.ReadBytes(packetLength - 12);
var checksum = (int)binaryReader.ReadInt32();
var checksum = binaryReader.ReadBytes(4);

var crc32 = new CRC32();
crc32.SlurpBlock(body, 0, packetLength - 4);
var validChecksum = crc32.Crc32Result;
var crc32 = new Crc32();
var computedChecksum = crc32.ComputeHash(body, 0, packetLength - 4).Reverse();

if (checksum != validChecksum)
if (!checksum.SequenceEqual(computedChecksum))
{
throw new InvalidOperationException("invalid checksum! skip");
}
Expand Down
10 changes: 5 additions & 5 deletions TLSharp.Core/Network/TcpTransport.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using TLSharp.Core.MTProto.Crypto;

namespace TLSharp.Core.Network
{
Expand Down Expand Up @@ -73,18 +75,16 @@ public TcpTransport(string address, int port, TcpClientConnectionHandler handler
var crcBytes = new byte[4];
if (await stream.ReadAsync(crcBytes, 0, 4, token).ConfigureAwait(false) != 4)
throw new InvalidOperationException("Couldn't read the crc");
int checksum = BitConverter.ToInt32(crcBytes, 0);

byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];

Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
var crc32 = new Ionic.Crc.CRC32();
crc32.SlurpBlock(rv, 0, rv.Length);
var validChecksum = crc32.Crc32Result;
var crc32 = new Crc32();
var computedChecksum = crc32.ComputeHash(rv).Reverse();

if (checksum != validChecksum)
if (!crcBytes.SequenceEqual(computedChecksum))
{
throw new InvalidOperationException("invalid checksum! skip");
}
Expand Down
4 changes: 0 additions & 4 deletions TLSharp.Core/TLSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<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.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
2 changes: 0 additions & 2 deletions TLSharp.Core/TLSharp.Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -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>
</metadata>
<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" />
</files>
</package>
2 changes: 0 additions & 2 deletions TLSharp.Core/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.11.0" targetFramework="net451" />
<package id="MarkerMetro.Unity.Ionic.Zlib" version="2.0.0.14" targetFramework="net452" />
</packages>
25 changes: 1 addition & 24 deletions TeleSharp.TL/TLUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BigMath;
using BigMath.Utils;

using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -159,26 +158,4 @@ public static void Serialize(long src, BinaryWriter writer)
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));
}
}
}
Loading

0 comments on commit 6d2c77f

Please sign in to comment.