diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.cs index accac077..e3ece7db 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.cs @@ -1124,9 +1124,9 @@ namespace SabreTools.Serialization.Wrappers // If we had the decompression DLL included, it's zlib if (FindResourceByNamedType("99, 1").Count > 0) - data = ExtractCExeZlib(resource); + data = DecompressCExeZlib(resource); else - data = ExtractCExeLZ(resource); + data = DecompressCExeLZ(resource); // If we have no data if (data == null) @@ -1152,75 +1152,6 @@ namespace SabreTools.Serialization.Wrappers } } - /// - /// Extract CExe data compressed with LZ - /// - /// Resource data to inflate - /// Inflated data on success, null otherwise - private byte[]? ExtractCExeLZ(byte[] resource) - { - try - { - var decompressor = IO.Compression.SZDD.Decompressor.CreateSZDD(resource); - using var dataStream = new MemoryStream(); - decompressor.CopyTo(dataStream); - return dataStream.ToArray(); - } - catch - { - // Reset the data - return null; - } - } - - /// - /// Extract CExe data compressed with zlib - /// - /// Resource data to inflate - /// Inflated data on success, null otherwise - private byte[]? ExtractCExeZlib(byte[] resource) - { - try - { - // Inflate the data into the buffer - var zstream = new ZLib.z_stream_s(); - byte[] data = new byte[resource.Length * 4]; - unsafe - { - fixed (byte* payloadPtr = resource) - fixed (byte* dataPtr = data) - { - zstream.next_in = payloadPtr; - zstream.avail_in = (uint)resource.Length; - zstream.total_in = (uint)resource.Length; - zstream.next_out = dataPtr; - zstream.avail_out = (uint)data.Length; - zstream.total_out = 0; - - ZLib.inflateInit_(zstream, ZLib.zlibVersion(), resource.Length); - int zret = ZLib.inflate(zstream, 1); - ZLib.inflateEnd(zstream); - } - } - - // Trim the buffer to the proper size - uint read = zstream.total_out; -#if NETFRAMEWORK - var temp = new byte[read]; - Array.Copy(data, temp, read); - data = temp; -#else - data = new ReadOnlySpan(data, 0, (int)read).ToArray(); -#endif - return data; - } - catch - { - // Reset the data - return null; - } - } - /// /// Extract data from the overlay /// @@ -1346,6 +1277,75 @@ namespace SabreTools.Serialization.Wrappers } } + /// + /// Decompress CExe data compressed with LZ + /// + /// Resource data to inflate + /// Inflated data on success, null otherwise + private byte[]? DecompressCExeLZ(byte[] resource) + { + try + { + var decompressor = IO.Compression.SZDD.Decompressor.CreateSZDD(resource); + using var dataStream = new MemoryStream(); + decompressor.CopyTo(dataStream); + return dataStream.ToArray(); + } + catch + { + // Reset the data + return null; + } + } + + /// + /// Decompress CExe data compressed with zlib + /// + /// Resource data to inflate + /// Inflated data on success, null otherwise + private byte[]? DecompressCExeZlib(byte[] resource) + { + try + { + // Inflate the data into the buffer + var zstream = new ZLib.z_stream_s(); + byte[] data = new byte[resource.Length * 4]; + unsafe + { + fixed (byte* payloadPtr = resource) + fixed (byte* dataPtr = data) + { + zstream.next_in = payloadPtr; + zstream.avail_in = (uint)resource.Length; + zstream.total_in = (uint)resource.Length; + zstream.next_out = dataPtr; + zstream.avail_out = (uint)data.Length; + zstream.total_out = 0; + + ZLib.inflateInit_(zstream, ZLib.zlibVersion(), resource.Length); + int zret = ZLib.inflate(zstream, 1); + ZLib.inflateEnd(zstream); + } + } + + // Trim the buffer to the proper size + uint read = zstream.total_out; +#if NETFRAMEWORK + var temp = new byte[read]; + Array.Copy(data, temp, read); + data = temp; +#else + data = new ReadOnlySpan(data, 0, (int)read).ToArray(); +#endif + return data; + } + catch + { + // Reset the data + return null; + } + } + #endregion #region Resource Data