mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-24 16:05:27 +00:00
Change more ReadBytes
This commit is contained in:
@@ -62,7 +62,8 @@ namespace SharpCompress.Common.Rar.Headers
|
||||
|
||||
if (headerSizeDiff > 0)
|
||||
{
|
||||
reader.ReadBytes(headerSizeDiff);
|
||||
using (reader.ReadScope(headerSizeDiff))
|
||||
{ }
|
||||
}
|
||||
|
||||
return header;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<byte>();
|
||||
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<byte>();
|
||||
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()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Org.BouncyCastle.Crypto
|
||||
using SharpCompress.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto
|
||||
{
|
||||
/// <remarks>Base interface for a symmetric key block cipher.</remarks>
|
||||
public interface IBlockCipher
|
||||
@@ -18,13 +20,11 @@
|
||||
bool IsPartialBlockOkay { get; }
|
||||
|
||||
/// <summary>Process a block.</summary>
|
||||
/// <param name="inBuf">The input buffer.</param>
|
||||
/// <param name="inOff">The offset into <paramref>inBuf</paramref> that the input block begins.</param>
|
||||
/// <param name="outBuf">The output buffer.</param>
|
||||
/// <param name="outOff">The offset into <paramref>outBuf</paramref> to write the output block.</param>
|
||||
/// <param name="input">The input buffer.</param>
|
||||
/// <param name="output">The output buffer.</param>
|
||||
/// <exception cref="DataLengthException">If input block is wrong size, or outBuf too small.</exception>
|
||||
/// <returns>The number of bytes processed and produced.</returns>
|
||||
int ProcessBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff);
|
||||
int ProcessBlock(ByteArrayPoolScope input, ByteArrayPoolScope output);
|
||||
|
||||
/// <summary>
|
||||
/// Reset the cipher to the same state as it was after the last init (if there was one).
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user