From 0dfdba3ea122f99c2ee44082ebd0847dc345fff8 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 7 Oct 2016 16:02:08 +0100 Subject: [PATCH] Change more ReadBytes --- .../Common/Rar/Headers/RarHeader.cs | 3 +- .../Common/Rar/Headers/RarHeaderFactory.cs | 58 ++++++++++--------- .../Common/Rar/RarCryptoBinaryReader.cs | 31 ++++++++-- .../Common/Rar/RarCryptoWrapper.cs | 19 +++--- src/SharpCompress/Common/Rar/RarRijndael.cs | 27 +++++---- src/SharpCompress/Crypto/IBlockCipher.cs | 12 ++-- src/SharpCompress/Crypto/RijndaelEngine.cs | 15 ++--- 7 files changed, 97 insertions(+), 68 deletions(-) diff --git a/src/SharpCompress/Common/Rar/Headers/RarHeader.cs b/src/SharpCompress/Common/Rar/Headers/RarHeader.cs index ecfaad65..6ca2105c 100644 --- a/src/SharpCompress/Common/Rar/Headers/RarHeader.cs +++ b/src/SharpCompress/Common/Rar/Headers/RarHeader.cs @@ -62,7 +62,8 @@ namespace SharpCompress.Common.Rar.Headers if (headerSizeDiff > 0) { - reader.ReadBytes(headerSizeDiff); + using (reader.ReadScope(headerSizeDiff)) + { } } return header; diff --git a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs index 8d9f2412..7379fc1c 100644 --- a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs +++ b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs @@ -52,35 +52,39 @@ namespace SharpCompress.Common.Rar.Headers if (firstByte == 0x52) { MemoryStream buffer = new MemoryStream(); - byte[] nextThreeBytes = reader.ReadBytes(3); - if ((nextThreeBytes[0] == 0x45) - && (nextThreeBytes[1] == 0x7E) - && (nextThreeBytes[2] == 0x5E)) + using (var nextThreeBytes = reader.ReadScope(3)) { - //old format and isvalid - buffer.WriteByte(0x52); - buffer.Write(nextThreeBytes, 0, 3); - rewindableStream.Rewind(buffer); - break; + if ((nextThreeBytes[0] == 0x45) + && (nextThreeBytes[1] == 0x7E) + && (nextThreeBytes[2] == 0x5E)) + { + //old format and isvalid + buffer.WriteByte(0x52); + buffer.Write(nextThreeBytes.Array, 0, 3); + rewindableStream.Rewind(buffer); + break; + } + using (var secondThreeBytes = reader.ReadScope(3)) + { + if ((nextThreeBytes[0] == 0x61) + && (nextThreeBytes[1] == 0x72) + && (nextThreeBytes[2] == 0x21) + && (secondThreeBytes[0] == 0x1A) + && (secondThreeBytes[1] == 0x07) + && (secondThreeBytes[2] == 0x00)) + { + //new format and isvalid + buffer.WriteByte(0x52); + buffer.Write(nextThreeBytes.Array, 0, 3); + buffer.Write(secondThreeBytes.Array, 0, 3); + rewindableStream.Rewind(buffer); + break; + } + buffer.Write(nextThreeBytes.Array, 0, 3); + buffer.Write(secondThreeBytes.Array, 0, 3); + rewindableStream.Rewind(buffer); + } } - byte[] secondThreeBytes = reader.ReadBytes(3); - if ((nextThreeBytes[0] == 0x61) - && (nextThreeBytes[1] == 0x72) - && (nextThreeBytes[2] == 0x21) - && (secondThreeBytes[0] == 0x1A) - && (secondThreeBytes[1] == 0x07) - && (secondThreeBytes[2] == 0x00)) - { - //new format and isvalid - buffer.WriteByte(0x52); - buffer.Write(nextThreeBytes, 0, 3); - buffer.Write(secondThreeBytes, 0, 3); - rewindableStream.Rewind(buffer); - break; - } - buffer.Write(nextThreeBytes, 0, 3); - buffer.Write(secondThreeBytes, 0, 3); - rewindableStream.Rewind(buffer); } if (count > MAX_SFX_SIZE) { diff --git a/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs b/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs index 3c024735..ce2e5aa1 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs @@ -1,6 +1,5 @@  #if !NO_CRYPTO -using System; using System.Collections.Generic; using System.IO; using SharpCompress.IO; @@ -57,12 +56,13 @@ namespace SharpCompress.Common.Rar int alignedSize = sizeToRead + ((~sizeToRead + 1) & 0xf); for (int i = 0; i < alignedSize / 16; i++) { - //long ax = System.currentTimeMillis(); - byte[] cipherText = base.ReadBytes(16); - var readBytes = rijndael.ProcessBlock(cipherText); - foreach (var readByte in readBytes) + using (var cipherText = PrivateReadScope(16)) { - data.Enqueue(readByte); + var readBytes = rijndael.ProcessBlock(cipherText); + foreach (var readByte in readBytes) + { + data.Enqueue(readByte); + } } } } @@ -74,6 +74,25 @@ namespace SharpCompress.Common.Rar return count; } + + private ByteArrayPoolScope PrivateReadScope(int count) + { + var scope = ByteArrayPool.RentScope(count); + int numRead = 0; + do + { + int n = base.Read(scope.Array, numRead, count); + if (n == 0) + { + break; + } + numRead += n; + count -= n; + } while (count > 0); + scope.OverrideSize(numRead); + return scope; + } + public void ClearQueue() { data.Clear(); diff --git a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs index 2a14ae1a..a5893cd7 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; +using SharpCompress.IO; namespace SharpCompress.Common.Rar { @@ -55,17 +56,21 @@ namespace SharpCompress.Common.Rar for (int i = 0; i < alignedSize / 16; i++) { //long ax = System.currentTimeMillis(); - byte[] cipherText = new byte[RarRijndael.CRYPTO_BLOCK_SIZE]; - actualStream.Read(cipherText, 0, RarRijndael.CRYPTO_BLOCK_SIZE); - - var readBytes = rijndael.ProcessBlock(cipherText); - foreach (var readByte in readBytes) - data.Enqueue(readByte); - + using (var cipherText = ByteArrayPool.RentScope(RarRijndael.CRYPTO_BLOCK_SIZE)) + { + actualStream.Read(cipherText.Array, 0, RarRijndael.CRYPTO_BLOCK_SIZE); + var readBytes = rijndael.ProcessBlock(cipherText); + foreach (var readByte in readBytes) + { + data.Enqueue(readByte); + } + } } for (int i = 0; i < count; i++) + { buffer[offset + i] = data.Dequeue(); + } } return count; } diff --git a/src/SharpCompress/Common/Rar/RarRijndael.cs b/src/SharpCompress/Common/Rar/RarRijndael.cs index b2ce22d6..3b4409a1 100644 --- a/src/SharpCompress/Common/Rar/RarRijndael.cs +++ b/src/SharpCompress/Common/Rar/RarRijndael.cs @@ -6,6 +6,7 @@ using System.Security.Cryptography; using System.Text; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; +using SharpCompress.IO; namespace SharpCompress.Common.Rar { @@ -96,22 +97,24 @@ namespace SharpCompress.Common.Rar return rijndael; } - public byte[] ProcessBlock(byte[] cipherText) + public byte[] ProcessBlock(ByteArrayPoolScope cipherText) { - var plainText = new byte[CRYPTO_BLOCK_SIZE]; - var decryptedBytes = new List(); - rijndael.ProcessBlock(cipherText, 0, plainText, 0); - - for (int j = 0; j < plainText.Length; j++) + using (var plainText = ByteArrayPool.RentScope(CRYPTO_BLOCK_SIZE)) { - decryptedBytes.Add((byte) (plainText[j] ^ aesInitializationVector[j%16])); //32:114, 33:101 - } + var decryptedBytes = new List(); + rijndael.ProcessBlock(cipherText, plainText); - for (int j = 0; j < aesInitializationVector.Length; j++) - { - aesInitializationVector[j] = cipherText[j]; + for (int j = 0; j < plainText.Count; j++) + { + decryptedBytes.Add((byte)(plainText[j] ^ aesInitializationVector[j % 16])); //32:114, 33:101 + } + + for (int j = 0; j < aesInitializationVector.Length; j++) + { + aesInitializationVector[j] = cipherText[j]; + } + return decryptedBytes.ToArray(); } - return decryptedBytes.ToArray(); } public void Dispose() diff --git a/src/SharpCompress/Crypto/IBlockCipher.cs b/src/SharpCompress/Crypto/IBlockCipher.cs index a4f30012..526b9c9e 100644 --- a/src/SharpCompress/Crypto/IBlockCipher.cs +++ b/src/SharpCompress/Crypto/IBlockCipher.cs @@ -1,4 +1,6 @@ -namespace Org.BouncyCastle.Crypto +using SharpCompress.IO; + +namespace Org.BouncyCastle.Crypto { /// Base interface for a symmetric key block cipher. public interface IBlockCipher @@ -18,13 +20,11 @@ bool IsPartialBlockOkay { get; } /// Process a block. - /// The input buffer. - /// The offset into inBuf that the input block begins. - /// The output buffer. - /// The offset into outBuf to write the output block. + /// The input buffer. + /// The output buffer. /// If input block is wrong size, or outBuf too small. /// The number of bytes processed and produced. - int ProcessBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff); + int ProcessBlock(ByteArrayPoolScope input, ByteArrayPoolScope output); /// /// Reset the cipher to the same state as it was after the last init (if there was one). diff --git a/src/SharpCompress/Crypto/RijndaelEngine.cs b/src/SharpCompress/Crypto/RijndaelEngine.cs index 7aca7aea..5506d9ea 100644 --- a/src/SharpCompress/Crypto/RijndaelEngine.cs +++ b/src/SharpCompress/Crypto/RijndaelEngine.cs @@ -1,5 +1,6 @@ using System; using Org.BouncyCastle.Crypto.Parameters; +using SharpCompress.IO; namespace Org.BouncyCastle.Crypto.Engines { @@ -586,28 +587,24 @@ namespace Org.BouncyCastle.Crypto.Engines return BC / 2; } - public int ProcessBlock( - byte[] input, - int inOff, - byte[] output, - int outOff) + public int ProcessBlock(ByteArrayPoolScope input, ByteArrayPoolScope output) { if (workingKey == null) { throw new InvalidOperationException("Rijndael engine not initialised"); } - if ((inOff + (BC / 2)) > input.Length) + if ((input.Offset + (BC / 2)) > input.Count) { throw new DataLengthException("input buffer too short"); } - if ((outOff + (BC / 2)) > output.Length) + if ((output.Offset + (BC / 2)) > output.Count) { throw new DataLengthException("output buffer too short"); } - UnPackBlock(input, inOff); + UnPackBlock(input.Array, input.Offset); if (forEncryption) { @@ -618,7 +615,7 @@ namespace Org.BouncyCastle.Crypto.Engines DecryptBlock(workingKey); } - PackBlock(output, outOff); + PackBlock(output.Array, output.Offset); return BC / 2; }