From d64c5c0c1e57e57a03149e3af3cd092489e9387f Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Wed, 26 Oct 2022 17:33:06 +0200 Subject: [PATCH] closes #103: files incorrectly padded to nearest 16 bytes --- EXAMPLES.md | 2 +- src/Client.Helpers.cs | 2 +- src/Encryption.cs | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index eb91e4b..e57d7fb 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -177,7 +177,7 @@ var uploadedFile = await client.UploadFileAsync(@"C:\Pictures\flower.jpg"); // Photo 3 specified by external url: const string photoUrl = "https://picsum.photos/310/200.jpg"; -var inputMedias = new InputMedia[] +var inputMedias = new List { photoFromTelegram, // PhotoBase has implicit conversion to InputMediaPhoto new InputMediaUploadedPhoto { file = uploadedFile }, diff --git a/src/Client.Helpers.cs b/src/Client.Helpers.cs index 980d9f1..b809f94 100644 --- a/src/Client.Helpers.cs +++ b/src/Client.Helpers.cs @@ -86,7 +86,7 @@ namespace WTelegram bool abort = false; for (long bytesLeft = length; !abort && bytesLeft != 0; file_part++) { - var bytes = new byte[(Math.Min(FilePartSize, bytesLeft) + 15) & ~15]; + var bytes = new byte[Math.Min(FilePartSize, bytesLeft)]; read = await stream.FullReadAsync(bytes, bytes.Length, default); await _parallelTransfers.WaitAsync(); bytesLeft -= read; diff --git a/src/Encryption.cs b/src/Encryption.cs index 72bf6fe..450e35f 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -539,6 +539,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB else { prevBytes = new byte[32]; Array.Copy(iv, 0, prevBytes, 16, 16); Array.Copy(iv, 16, prevBytes, 0, 16); } } + public override long Length => base.Length + 15 & ~15; public override bool CanSeek => false; public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); @@ -549,7 +550,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB Process(buffer, offset, count); if (ContentLength.HasValue && _innerStream.Position == _innerStream.Length) return count - (int)(_innerStream.Position - ContentLength.Value); - return count; + return count + 15 & ~15; } public override void Write(byte[] buffer, int offset, int count) @@ -562,7 +563,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB public void Process(byte[] buffer, int offset, int count) { - count = (count + 15) & ~15; + count = count + 15 & ~15; var span = MemoryMarshal.Cast(buffer.AsSpan(offset, count)); var prev = MemoryMarshal.Cast(prevBytes); for (offset = 0, count /= 8; offset < count;)