Browse Source

Merge pull request #2471 from cpraehaus/load-xamarin-compressed-assemblies

Fix #2137: Support loading compressed Xamarin assemblies
pull/2479/head
Siegfried Pammer 4 years ago committed by GitHub
parent
commit
7473ff238b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      ILSpy/ILSpy.csproj
  2. 48
      ILSpy/LoadedAssembly.cs
  3. 28
      doc/third-party-notices.txt

2
ILSpy/ILSpy.csproj

@ -54,6 +54,8 @@ @@ -54,6 +54,8 @@
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.31" />
<PackageReference Include="TomsToolbox.Wpf.Styles" Version="$(WpfStylesToolboxVersion)" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.2.6" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
</ItemGroup>
<ItemGroup>

48
ILSpy/LoadedAssembly.cs

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@ -35,6 +36,8 @@ using ICSharpCode.Decompiler.TypeSystem.Implementation; @@ -35,6 +36,8 @@ using ICSharpCode.Decompiler.TypeSystem.Implementation;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.ILSpy.Options;
using K4os.Compression.LZ4;
#nullable enable
namespace ICSharpCode.ILSpy
@ -323,6 +326,15 @@ namespace ICSharpCode.ILSpy @@ -323,6 +326,15 @@ namespace ICSharpCode.ILSpy
{
loadAssemblyException = ex;
}
// Maybe its a compressed Xamarin/Mono assembly, see https://github.com/xamarin/xamarin-android/pull/4686
try
{
return LoadCompressedAssembly(fileName);
}
catch (InvalidDataException)
{
// Not a compressed module, try other options below
}
// If it's not a .NET module, maybe it's a single-file bundle
var bundle = LoadedPackage.FromBundle(fileName);
if (bundle != null)
@ -365,6 +377,42 @@ namespace ICSharpCode.ILSpy @@ -365,6 +377,42 @@ namespace ICSharpCode.ILSpy
return new LoadResult(module);
}
LoadResult LoadCompressedAssembly(string fileName)
{
const uint CompressedDataMagic = 0x5A4C4158; // Magic used for Xamarin compressed module header ('XALZ', little-endian)
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (var fileReader = new BinaryReader(fileStream))
{
// Read compressed file header
var magic = fileReader.ReadUInt32();
if (magic != CompressedDataMagic)
throw new InvalidDataException($"Xamarin compressed module header magic {magic} does not match expected {CompressedDataMagic}");
_ = fileReader.ReadUInt32(); // skip index into descriptor table, unused
int uncompressedLength = (int)fileReader.ReadUInt32();
int compressedLength = (int)fileStream.Length; // Ensure we read all of compressed data
ArrayPool<byte> pool = ArrayPool<byte>.Shared;
var src = pool.Rent(compressedLength);
var dst = pool.Rent(uncompressedLength);
try
{
// fileReader stream position is now at compressed module data
fileStream.Read(src, 0, compressedLength);
// Decompress
LZ4Codec.Decode(src, 0, compressedLength, dst, 0, uncompressedLength);
// Load module from decompressed data buffer
using (var uncompressedStream = new MemoryStream(dst, writable: false))
{
return LoadAssembly(uncompressedStream, PEStreamOptions.PrefetchEntireImage);
}
}
finally
{
pool.Return(dst);
pool.Return(src);
}
}
}
IDebugInfoProvider? LoadDebugInfo(PEFile module)
{
if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols)

28
doc/third-party-notices.txt

@ -415,6 +415,34 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE @@ -415,6 +415,34 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
License Notice for K4os.Compression.LZ4 (part of ILSpy)
---------------------------
https://github.com/MiloszKrajewski/K4os.Compression.LZ4/blob/master/LICENSE
MIT License
Copyright (c) 2017 Milosz Krajewski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
License Notice for LightJson (part of ICSharpCode.Decompiler)
---------------------------

Loading…
Cancel
Save