Skip to content

Commit 3620948

Browse files
RampastringNayiemW
andcommitted
Use ReadExactly instead of Read where we expect to read a specific number of bytes
Browser virtual file systems might not always read the specified number of bytes when regular Read is called Co-authored-by: Nayiem Willems <nayiemwillems@gmail.com>
1 parent ac5e424 commit 3620948

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

src/TSMapEditor/CCEngine/CCCrypto.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,12 @@ public override int Read(byte[] buffer, int offset, int count)
309309
}
310310

311311
// Read new encrypted data to staging buffer.
312-
int read = stream.Read(stagingBuffer, stagingBufferOffset, sizeToRead);
313-
314-
if ((read & (SIZE_OF_BLOCK - 1)) != 0)
315-
throw new System.ArgumentException("Number of bytes read from encapsulated stream isn't a multiple of Blowfish block size");
312+
stream.ReadExactly(stagingBuffer, stagingBufferOffset, sizeToRead);
316313

317314
int dataStart = stagingBufferOffset - stagingRemainingBytes;
318315

319316
// Decrypt blocks in-place in staging buffer.
320-
for (int i = 0; i < read; i += SIZE_OF_BLOCK)
317+
for (int i = 0; i < sizeToRead; i += SIZE_OF_BLOCK)
321318
{
322319
DecryptBlock();
323320
stagingBufferOffset += SIZE_OF_BLOCK;

src/TSMapEditor/CCEngine/MixFile.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,19 @@ public void Parse(Stream stream = null)
8787
entries = new List<MixFileEntry>();
8888

8989
byte[] buffer = new byte[256];
90-
stream.Read(buffer, 0, 4);
90+
stream.ReadExactly(buffer, 0, 4);
9191
MixType mixType = (MixType)BitConverter.ToInt32(buffer, 0);
9292

9393
bool isEncrypted = (mixType & MixType.ENCRYPTED) != 0;
9494

9595
if (isEncrypted)
9696
{
9797
// Read and decrypt the Blowfish associated with this MIX.
98-
stream.Read(buffer, 0, KeyDecryptor.SIZE_OF_ENCRYPTED_KEY);
98+
stream.ReadExactly(buffer, 0, KeyDecryptor.SIZE_OF_ENCRYPTED_KEY);
9999
stream = new BlowfishStream(stream, KeyDecryptor.DecryptBlowfishKey(buffer));
100100
}
101101

102-
stream.Read(buffer, 0, MixFileHeader.SIZE_OF_HEADER);
102+
stream.ReadExactly(buffer, 0, MixFileHeader.SIZE_OF_HEADER);
103103

104104
MixFileHeader header = new MixFileHeader(buffer);
105105

@@ -117,7 +117,7 @@ public void Parse(Stream stream = null)
117117
if (stream.Position + MixFileEntry.SIZE_OF_FILE_ENTRY >= stream.Length)
118118
throw new MixParseException("Invalid MIX file.");
119119

120-
stream.Read(buffer, 0, MixFileEntry.SIZE_OF_FILE_ENTRY);
120+
stream.ReadExactly(buffer, 0, MixFileEntry.SIZE_OF_FILE_ENTRY);
121121
entries.Add(new MixFileEntry(buffer));
122122
}
123123

@@ -173,7 +173,7 @@ public byte[] GetFileData(int offset, int count)
173173
byte[] buffer = new byte[count];
174174

175175
stream.Position = mixStartOffset + bodyOffset + offset;
176-
stream.Read(buffer, 0, count);
176+
stream.ReadExactly(buffer, 0, count);
177177

178178
return buffer;
179179
}

0 commit comments

Comments
 (0)