From 78a0a47c50e1de1e81f9a8a05a0ba23baa285900 Mon Sep 17 00:00:00 2001 From: Wizou Date: Mon, 27 Sep 2021 03:25:28 +0200 Subject: [PATCH] Fix serialization issue with null string. DownloadFileAsync for documents now returns the MIME type (more accurate) --- src/Client.cs | 30 ++++++++++++++++++------------ src/TL.cs | 8 ++++---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Client.cs b/src/Client.cs index 229cfdf..10148f4 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -201,7 +201,11 @@ namespace WTelegram { await Task.Delay(60000, ct); if (_saltChangeCounter > 0) --_saltChangeCounter; +#if DEBUG + await this.PingDelayDisconnect(ping_id++, 300); +#else await this.PingDelayDisconnect(ping_id++, 75); +#endif } } @@ -392,12 +396,12 @@ namespace WTelegram { using var clearStream = new MemoryStream(1024); using var clearWriter = new BinaryWriter(clearStream, Encoding.UTF8); - #if MTPROTO1 +#if MTPROTO1 const int prepend = 0; - #else +#else const int prepend = 32; clearWriter.Write(DCSession.AuthKey, 88, prepend); - #endif +#endif clearWriter.Write(DCSession.Salt); // int64 salt clearWriter.Write(_session.Id); // int64 session_id clearWriter.Write(msgId); // int64 message_id @@ -410,20 +414,20 @@ namespace WTelegram Helpers.Log(1, $"Sending {typeName,-50} {_session.MsgIdToStamp(msgId):u} (svc)"); int clearLength = (int)clearStream.Length - prepend; // length before padding (= 32 + message_data_length) int padding = (0x7FFFFFF0 - clearLength) % 16; - #if !MTPROTO1 +#if !MTPROTO1 padding += _random.Next(1, 64) * 16; // MTProto 2.0 padding must be between 12..1024 with total length divisible by 16 - #endif +#endif clearStream.SetLength(prepend + clearLength + padding); byte[] clearBuffer = clearStream.GetBuffer(); BinaryPrimitives.WriteInt32LittleEndian(clearBuffer.AsSpan(prepend + 28), clearLength - 32); // patch message_data_length RNG.GetBytes(clearBuffer, prepend + clearLength, padding); - #if MTPROTO1 +#if MTPROTO1 var msgKeyLarge = Sha1.ComputeHash(clearBuffer, 0, clearLength); // padding excluded from computation! const int msgKeyOffset = 4; // msg_key = low 128-bits of SHA1(plaintext) - #else +#else var msgKeyLarge = Sha256.ComputeHash(clearBuffer, 0, prepend + clearLength + padding); const int msgKeyOffset = 8; // msg_key = middle 128-bits of SHA256(authkey_part+plaintext+padding) - #endif +#endif byte[] encrypted_data = EncryptDecryptMessage(clearBuffer.AsSpan(prepend, clearLength + padding), true, DCSession.AuthKey, msgKeyLarge, msgKeyOffset); writer.Write(DCSession.AuthKeyID); // int64 auth_key_id @@ -807,7 +811,7 @@ namespace WTelegram return user; } - #region TL-Helpers +#region TL-Helpers /// Helper function to upload a file to Telegram /// an or than can be used in various requests public Task UploadFileAsync(string pathname) @@ -923,10 +927,12 @@ namespace WTelegram /// Download given photo from Telegram into the outputStream /// stream to write to. This method does not close/dispose the stream /// if specified, will download the given thumbnail instead of the full document - public async Task DownloadFileAsync(Document document, Stream outputStream, PhotoSizeBase thumbSize = null) + /// MIME type of the document or thumbnail + public async Task DownloadFileAsync(Document document, Stream outputStream, PhotoSizeBase thumbSize = null) { var fileLocation = document.ToFileLocation(thumbSize); - return await DownloadFileAsync(fileLocation, outputStream, document.dc_id); + var fileType = await DownloadFileAsync(fileLocation, outputStream, document.dc_id); + return thumbSize == null ? document.mime_type : "image/" + fileType; } /// Download given file from Telegram into the outputStream @@ -958,7 +964,7 @@ namespace WTelegram } return fileData.type; } - #endregion +#endregion /// Enable the collection of id/access_hash pairs (experimental) public bool CollectAccessHash { get; set; } diff --git a/src/TL.cs b/src/TL.cs index f199ce9..9e1099e 100644 --- a/src/TL.cs +++ b/src/TL.cs @@ -231,11 +231,11 @@ namespace TL internal static void WriteTLNull(this BinaryWriter writer, Type type) { - if (!type.IsArray) - writer.Write(Layer.NullCtor); + if (type == typeof(string)) { } + else if (!type.IsArray) { writer.Write(Layer.NullCtor); return; } else if (type != typeof(byte[])) - writer.Write(Layer.VectorCtor); - writer.Write(0); // null arrays are serialized as empty + writer.Write(Layer.VectorCtor); // not raw bytes but a vector => needs a VectorCtor + writer.Write(0); // null arrays/strings are serialized as empty } internal static ITLObject UnzipPacket(GzipPacked obj, WTelegram.Client client)