Cleanup to get some things working

This commit is contained in:
Matt Nadareski
2022-05-21 23:44:03 -07:00
parent 93263dfeb9
commit 80626f3b9e
10 changed files with 109 additions and 175 deletions

View File

@@ -147,8 +147,8 @@ namespace LibMSPackSharp.CAB
// Free folder decompression state if it has been decompressed
if (State != null && (State.Folder == fol))
{
if (State.InputFileHandle != null)
System.Close(State.InputFileHandle);
System.Close(State.InputFileHandle);
System.Close(State.OutputFileHandle);
FreeDecompressionState();
State = null;
@@ -373,8 +373,8 @@ namespace LibMSPackSharp.CAB
if (State.InputFileHandle == null || (fol.Data.Cab != State.InputCabinet))
{
// Close previous file handle if from a different cab
if (State.InputFileHandle != null)
System.Close(State.InputFileHandle);
System.Close(State.InputFileHandle);
System.Close(State.OutputFileHandle);
State.InputCabinet = fol.Data.Cab;
State.InputFileHandle = System.Open(fol.Data.Cab.Filename, OpenMode.MSPACK_SYS_OPEN_READ);
@@ -422,6 +422,9 @@ namespace LibMSPackSharp.CAB
State.OutputFileHandle = null;
if ((bytes = file.Header.FolderOffset - State.Offset) != 0)
{
State.OutputFileHandle = fh;
InitDecompressionState(fol.Header.CompType);
error = State.Decompress(State.DecompressorState, bytes);
Error = (error == Error.MSPACK_ERR_READ) ? ReadError : error;
}
@@ -439,7 +442,7 @@ namespace LibMSPackSharp.CAB
// Close output file
System.Close(fh);
State.OutputFileHandle = null;
System.Close(State.OutputFileHandle);
return Error;
}

View File

@@ -220,8 +220,8 @@ namespace LibMSPackSharp.CHM
// If this CHM was being decompressed, free decompression state
if (self.State != null && (self.State.Header == chm))
{
if (self.State.InputFileHandle != null)
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.OutputFileHandle);
self.State = null;
}
@@ -992,8 +992,8 @@ namespace LibMSPackSharp.CHM
// Open input chm file if not open, or the open one is a different chm
if (self.State.InputFileHandle == null || (self.State.Header != chm))
{
if (self.State.InputFileHandle != null)
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.OutputFileHandle);
self.State.Header = chm;
self.State.Offset = 0;

View File

@@ -185,17 +185,18 @@ namespace LibMSPackSharp.Compression
/// <returns>true for OK or false for error</returns>
public static bool MakeDecodeTable(int nsyms, int nbits, byte[] length, ushort[] table, bool msb)
{
int next_symbol;
long leaf, fill;
long reverse; // Only used when !msb
long pos = 0; // The current position in the decode table
int table_mask = 1 << nbits;
long bit_mask = table_mask >> 1; // Don't do 0 length codes
ushort sym, next_symbol;
uint leaf, fill;
uint reverse; // Only used when !msb
byte bit_num;
uint pos = 0; // The current position in the decode table
uint table_mask = (uint)1 << nbits;
uint bit_mask = table_mask >> 1; // Don't do 0 length codes
// Fill entries for codes short enough for a direct mapping
for (byte bit_num = 1; bit_num <= nbits; bit_num++)
for (bit_num = 1; bit_num <= nbits; bit_num++)
{
for (ushort sym = 0; sym < nsyms; sym++)
for (sym = 0; sym < nsyms; sym++)
{
if (length[sym] != bit_num)
continue;
@@ -233,7 +234,7 @@ namespace LibMSPackSharp.Compression
else
{
fill = bit_mask;
next_symbol = 1 << bit_num;
next_symbol = (ushort)(1 << bit_num);
do
{
@@ -251,7 +252,7 @@ namespace LibMSPackSharp.Compression
return true;
// Mark all remaining table entries as unused
for (long sym = pos; sym < table_mask; sym++)
for (sym = (ushort)pos; sym < table_mask; sym++)
{
if (msb)
{
@@ -261,7 +262,7 @@ namespace LibMSPackSharp.Compression
{
reverse = sym;
leaf = 0;
fill = nbits;
fill = (uint)nbits;
do
{
@@ -275,7 +276,7 @@ namespace LibMSPackSharp.Compression
}
// next_symbol = base of allocation for long codes
next_symbol = ((table_mask >> 1) < nsyms) ? nsyms : (table_mask >> 1);
next_symbol = ((table_mask >> 1) < nsyms) ? (ushort)nsyms : (ushort)(table_mask >> 1);
// Give ourselves room for codes to grow by up to 16 more bits.
// codes now start at bit nbits+16 and end at (nbits+16-codelength)
@@ -283,9 +284,9 @@ namespace LibMSPackSharp.Compression
table_mask <<= 16;
bit_mask = 1 << 15;
for (int bit_num = nbits + 1; bit_num <= HUFF_MAXBITS; bit_num++)
for (bit_num = (byte)(nbits + 1); bit_num <= HUFF_MAXBITS; bit_num++)
{
for (ushort sym = 0; sym < nsyms; sym++)
for (sym = 0; sym < nsyms; sym++)
{
if (length[sym] != bit_num)
continue;
@@ -301,7 +302,7 @@ namespace LibMSPackSharp.Compression
// leaf = the first nbits of the code, reversed
reverse = pos >> 16;
leaf = 0;
fill = nbits;
fill = (uint)nbits;
do
{
@@ -322,7 +323,7 @@ namespace LibMSPackSharp.Compression
}
// Follow the path and select either left or right for next bit
leaf = table[leaf] << 1;
leaf = (uint)(table[leaf] << 1);
if (((pos >> (15 - (int)fill)) & 1) != 0)
leaf++;
}

View File

@@ -289,7 +289,7 @@ namespace LibMSPackSharp.Compression
}
else
{
return zip.Error = (error > 0) ? error : Error.MSPACK_ERR_DECRUNCH;
return zip.Error = error;
}
}
@@ -733,10 +733,13 @@ namespace LibMSPackSharp.Compression
}
}
while (i < 19) bl_len[bitlen_order[i++]] = 0;
while (i < 19)
{
bl_len[bitlen_order[i++]] = 0;
}
// Create decoding table with an immediate lookup
if (CompressionStream.MakeDecodeTable(19, 7, bl_len, bl_table, msb: false))
if (!CompressionStream.MakeDecodeTable(19, 7, bl_len, bl_table, msb: false))
return Error.INF_ERR_BITLENTBL;
// Read literal / distance code lengths */
@@ -1113,9 +1116,11 @@ namespace LibMSPackSharp.Compression
}
// Get the length and its complement
length = (uint)(lens_buf[0] | (lens_buf[1] << 8));
i = (uint)(lens_buf[2] | (lens_buf[3] << 8));
if (length != (~i & 0xFFFF))
length = (ushort)(lens_buf[0] | (lens_buf[1] << 8));
i = (ushort)(lens_buf[2] | (lens_buf[3] << 8));
ushort compl = (ushort)(~i & 0xFFFF);
if (length != compl)
return Error.INF_ERR_COMPLEMENT;
// Read and copy the uncompressed data into the window
@@ -1218,10 +1223,10 @@ namespace LibMSPackSharp.Compression
// Now huffman lengths are read for either kind of block,
// create huffman decoding tables
if (CompressionStream.MakeDecodeTable(MSZIP_LITERAL_MAXSYMBOLS, MSZIP_LITERAL_TABLEBITS, zip.LITERAL_len, zip.LITERAL_table, msb: false))
if (!CompressionStream.MakeDecodeTable(MSZIP_LITERAL_MAXSYMBOLS, MSZIP_LITERAL_TABLEBITS, zip.LITERAL_len, zip.LITERAL_table, msb: false))
return Error.INF_ERR_LITERALTBL;
if (CompressionStream.MakeDecodeTable(MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_DISTANCE_TABLEBITS, zip.DISTANCE_len, zip.DISTANCE_table, msb: false))
if (!CompressionStream.MakeDecodeTable(MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_DISTANCE_TABLEBITS, zip.DISTANCE_len, zip.DISTANCE_table, msb: false))
return Error.INF_ERR_DISTANCETBL;
// Decode forever until end of block code

View File

@@ -191,9 +191,8 @@ namespace LibMSPackSharp
SystemImpl sys = self.System;
if (self.State != null)
{
if (self.State.InputFileHandle != null)
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.OutputFileHandle);
self.FreeDecompressionState();
}
}
@@ -261,8 +260,8 @@ namespace LibMSPackSharp
SystemImpl sys = self.System;
if (self.State != null)
{
if (self.State.InputFileHandle != null)
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.InputFileHandle);
sys.Close(self.State.OutputFileHandle);
}
}
}

View File

@@ -123,18 +123,14 @@ namespace LibMSPackSharp.OAB
if (infh == null)
{
ret = Error.MSPACK_ERR_OPEN;
if (infh != null)
sys.Close(infh);
sys.Close(infh);
return ret;
}
if (sys.Read(infh, hdrbuf, 0, oabhead_SIZEOF) != oabhead_SIZEOF)
{
ret = Error.MSPACK_ERR_READ;
if (infh != null)
sys.Close(infh);
sys.Close(infh);
return ret;
}
@@ -142,9 +138,7 @@ namespace LibMSPackSharp.OAB
BitConverter.ToUInt32(hdrbuf, oabhead_VersionLo) != 1)
{
ret = Error.MSPACK_ERR_SIGNATURE;
if (infh != null)
sys.Close(infh);
sys.Close(infh);
return ret;
}
@@ -155,11 +149,8 @@ namespace LibMSPackSharp.OAB
if (outfh == null)
{
ret = Error.MSPACK_ERR_OPEN;
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
@@ -182,11 +173,8 @@ namespace LibMSPackSharp.OAB
if (sys.Read(infh, buf, 0, oabblk_SIZEOF) != oabblk_SIZEOF)
{
ret = Error.MSPACK_ERR_READ;
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
@@ -198,11 +186,8 @@ namespace LibMSPackSharp.OAB
if (blk_dsize > block_max || blk_dsize > target_size || blk_flags > 1)
{
ret = Error.MSPACK_ERR_DATAFORMAT;
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
@@ -212,22 +197,16 @@ namespace LibMSPackSharp.OAB
if (blk_dsize != blk_csize)
{
ret = Error.MSPACK_ERR_DATAFORMAT;
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
ret = CopyFileHandle(sys, infh, outfh, (int)blk_dsize, buf, self.BufferSize);
if (ret != Error.MSPACK_ERR_OK)
{
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
}
@@ -248,22 +227,16 @@ namespace LibMSPackSharp.OAB
if (lzx == null)
{
ret = Error.MSPACK_ERR_NOMEMORY;
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
ret = LZX.Decompress(lzx, blk_dsize);
if (ret != Error.MSPACK_ERR_OK)
{
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
@@ -273,22 +246,16 @@ namespace LibMSPackSharp.OAB
ret = CopyFileHandle(sys, infh, null, in_ofh.Available, buf, self.BufferSize);
if (ret != Error.MSPACK_ERR_OK)
{
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
if (out_ofh.CRC != blk_crc)
{
ret = Error.MSPACK_ERR_CHECKSUM;
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
}
@@ -296,10 +263,8 @@ namespace LibMSPackSharp.OAB
target_size -= blk_dsize;
}
if (outfh != null)
sys.Close(outfh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(infh);
return ret;
}
@@ -326,18 +291,14 @@ namespace LibMSPackSharp.OAB
if (infh == null)
{
ret = Error.MSPACK_ERR_OPEN;
if (infh != null)
sys.Close(infh);
sys.Close(infh);
return ret;
}
if (sys.Read(infh, hdrbuf, 0, patchhead_SIZEOF) != patchhead_SIZEOF)
{
ret = Error.MSPACK_ERR_READ;
if (infh != null)
sys.Close(infh);
sys.Close(infh);
return ret;
}
@@ -345,9 +306,7 @@ namespace LibMSPackSharp.OAB
BitConverter.ToUInt32(hdrbuf, patchhead_VersionLo) != 2)
{
ret = Error.MSPACK_ERR_SIGNATURE;
if (infh != null)
sys.Close(infh);
sys.Close(infh);
return ret;
}
@@ -362,11 +321,8 @@ namespace LibMSPackSharp.OAB
if (basefh == null)
{
ret = Error.MSPACK_ERR_OPEN;
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
@@ -374,13 +330,9 @@ namespace LibMSPackSharp.OAB
if (outfh == null)
{
ret = Error.MSPACK_ERR_OPEN;
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
@@ -403,13 +355,9 @@ namespace LibMSPackSharp.OAB
if (sys.Read(infh, buf, 0, patchblk_SIZEOF) != patchblk_SIZEOF)
{
ret = Error.MSPACK_ERR_READ;
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
@@ -421,13 +369,9 @@ namespace LibMSPackSharp.OAB
if (blk_dsize > block_max || blk_dsize > target_size || blk_ssize > block_max)
{
ret = Error.MSPACK_ERR_DATAFORMAT;
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
@@ -445,39 +389,27 @@ namespace LibMSPackSharp.OAB
if (lzx == null)
{
ret = Error.MSPACK_ERR_NOMEMORY;
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
ret = LZX.SetReferenceData(lzx, sys, basefh, blk_ssize);
if (ret != Error.MSPACK_ERR_OK)
{
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
ret = LZX.Decompress(lzx, blk_dsize);
if (ret != Error.MSPACK_ERR_OK)
{
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
@@ -487,38 +419,27 @@ namespace LibMSPackSharp.OAB
ret = CopyFileHandle(sys, infh, null, in_ofh.Available, buf, self.BufferSize);
if (ret != Error.MSPACK_ERR_OK)
{
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
if (out_ofh.CRC != blk_crc)
{
ret = Error.MSPACK_ERR_CHECKSUM;
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}
target_size -= blk_dsize;
}
if (outfh != null)
sys.Close(outfh);
if (basefh != null)
sys.Close(basefh);
if (infh != null)
sys.Close(infh);
sys.Close(outfh);
sys.Close(basefh);
sys.Close(infh);
return ret;
}

View File

@@ -51,9 +51,7 @@ namespace LibMSPackSharp.SZDD
if (self.Error != Error.MSPACK_ERR_OK)
{
if (fh != null)
sys.Close(fh);
sys.Close(fh);
hdr = null;
}

View File

@@ -259,6 +259,7 @@ namespace LibMSPackSharp
catch { return -1; }
return bytes;
}
return -1;
}

View File

@@ -41,7 +41,9 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the decompressor
var decompressor = Library.CreateCABDecompressor(null);
var cabFile = decompressor.Open(file);
var sub = cabFile.Files;
@@ -66,6 +68,9 @@ namespace BurnOutSharp.FileType
sub = sub.Next;
}
// Destroy the decompressor
Library.DestroyCABDecompressor(decompressor);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -61,6 +61,7 @@ namespace BurnOutSharp.FileType
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
entry.WriteToFile(tempFile);
}
catch (Exception ex)