Skip to content

Commit 8a1e90f

Browse files
committed
Reduce unnecessary casts
1 parent ddc0094 commit 8a1e90f

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

SabreTools.Serialization/Wrappers/InstallShieldCabinet.Extraction.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,15 @@ public bool FileSave(int index, string filename, bool includeDebug, bool useOld
287287
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
288288
var md5 = new HashWrapper(HashType.MD5);
289289

290-
long readBytesLeft = (long)GetReadableBytes(fileDescriptor);
291-
long writeBytesLeft = (long)GetWritableBytes(fileDescriptor);
292-
byte[] inputBuffer;
290+
ulong readBytesLeft = GetReadableBytes(fileDescriptor);
291+
ulong writeBytesLeft = GetWritableBytes(fileDescriptor);
293292
byte[] outputBuffer = new byte[BUFFER_SIZE];
294293
long totalWritten = 0;
295294

296295
// Read while there are bytes remaining
297296
while (readBytesLeft > 0 && writeBytesLeft > 0)
298297
{
299-
long bytesToWrite = BUFFER_SIZE;
298+
uint bytesToWrite = BUFFER_SIZE;
300299
int result;
301300

302301
// Handle compressed files
@@ -318,7 +317,7 @@ public bool FileSave(int index, string filename, bool includeDebug, bool useOld
318317

319318
// Attempt to read the specified number of bytes
320319
uint bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
321-
inputBuffer = new byte[BUFFER_SIZE];
320+
byte[] inputBuffer = new byte[BUFFER_SIZE];
322321
if (!reader.Read(inputBuffer, 0, bytesToRead))
323322
{
324323
Console.Error.WriteLine($"Failed to read {lengthArr.Length} bytes of file {index} ({GetFileName(index)}) from input cabinet file {fileDescriptor.Volume}");
@@ -350,7 +349,7 @@ public bool FileSave(int index, string filename, bool includeDebug, bool useOld
350349
// Handle uncompressed files
351350
else
352351
{
353-
bytesToWrite = Math.Min(readBytesLeft, BUFFER_SIZE);
352+
bytesToWrite = (uint)Math.Min(readBytesLeft, BUFFER_SIZE);
354353
if (!reader.Read(outputBuffer, 0, (int)bytesToWrite))
355354
{
356355
Console.Error.WriteLine($"Failed to write {bytesToWrite} bytes from input cabinet file {fileDescriptor.Volume}");
@@ -360,11 +359,11 @@ public bool FileSave(int index, string filename, bool includeDebug, bool useOld
360359
}
361360

362361
// Set remaining bytes
363-
readBytesLeft -= (uint)bytesToWrite;
362+
readBytesLeft -= bytesToWrite;
364363
}
365364

366365
// Hash and write the next block
367-
bytesToWrite = Math.Min(bytesToWrite, writeBytesLeft);
366+
bytesToWrite = (uint)Math.Min(bytesToWrite, writeBytesLeft);
368367
md5.Process(outputBuffer, 0, (int)bytesToWrite);
369368
fs?.Write(outputBuffer, 0, (int)bytesToWrite);
370369

@@ -450,7 +449,7 @@ public bool FileSaveRaw(int index, string filename)
450449
/// <summary>
451450
/// Uncompress a source byte array to a destination
452451
/// </summary>
453-
private unsafe static int Uncompress(byte[] dest, ref long destLen, byte[] source, ref uint sourceLen)
452+
private unsafe static int Uncompress(byte[] dest, ref uint destLen, byte[] source, ref uint sourceLen)
454453
{
455454
fixed (byte* sourcePtr = source, destPtr = dest)
456455
{
@@ -459,7 +458,7 @@ private unsafe static int Uncompress(byte[] dest, ref long destLen, byte[] sourc
459458
next_in = sourcePtr,
460459
avail_in = sourceLen,
461460
next_out = destPtr,
462-
avail_out = (uint)destLen,
461+
avail_out = destLen,
463462
};
464463

465464
// make second parameter negative to disable checksum verification
@@ -483,7 +482,7 @@ private unsafe static int Uncompress(byte[] dest, ref long destLen, byte[] sourc
483482
/// <summary>
484483
/// Uncompress a source byte array to a destination (old version)
485484
/// </summary>
486-
private unsafe static int UncompressOld(byte[] dest, ref long destLen, byte[] source, ref uint sourceLen)
485+
private unsafe static int UncompressOld(byte[] dest, ref uint destLen, byte[] source, ref uint sourceLen)
487486
{
488487
fixed (byte* sourcePtr = source, destPtr = dest)
489488
{
@@ -492,7 +491,7 @@ private unsafe static int UncompressOld(byte[] dest, ref long destLen, byte[] so
492491
next_in = sourcePtr,
493492
avail_in = sourceLen,
494493
next_out = destPtr,
495-
avail_out = (uint)destLen,
494+
avail_out = destLen,
496495
};
497496

498497
destLen = 0;

0 commit comments

Comments
 (0)