-
Notifications
You must be signed in to change notification settings - Fork 578
[tools] Add a tool to decompress XA-compressed assemblies #5536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <Company>Microsoft Corporation</Company> | ||
| <Copyright>2021 Microsoft Corporation</Copyright> | ||
| <Version>0.0.1</Version> | ||
| <TargetFramework>net472</TargetFramework> | ||
| <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
| <RootNamespace>Xamarin.Android.Tools.DecompressAssemblies</RootNamespace> | ||
| <AssemblyName>decompress-assemblies</AssemblyName> | ||
| <OutputPath>../../bin/$(Configuration)/bin</OutputPath> | ||
| <OutputType>Exe</OutputType> | ||
| <LibZipSharpBundleAllNativeLibraries>true</LibZipSharpBundleAllNativeLibraries> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <Import Project="..\..\Configuration.props" /> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Xamarin.LibZipSharp" Version="$(LibZipSharpVersion)" /> | ||
| <PackageReference Include="K4os.Compression.LZ4" Version="$(LZ4PackageVersion)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Content Include="..\scripts\decompress-assemblies"> | ||
| <Link>decompress-assemblies</Link> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </Content> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using System.IO; | ||
|
|
||
| using K4os.Compression.LZ4; | ||
| using Xamarin.Tools.Zip; | ||
|
|
||
| namespace Xamarin.Android.Tools.DecompressAssemblies | ||
| { | ||
| class App | ||
| { | ||
| const uint CompressedDataMagic = 0x5A4C4158; // 'XALZ', little-endian | ||
|
|
||
| static readonly ArrayPool<byte> bytePool = ArrayPool<byte>.Shared; | ||
|
|
||
| static int Usage () | ||
| { | ||
| Console.WriteLine ("Usage: decompress-assemblies {file.{dll,apk,aab}} [{file.{dll,apk,aab} ...]"); | ||
| Console.WriteLine (); | ||
| Console.WriteLine ("DLL files passed on command are uncompressed to the current directory with the `uncompressed-` prefix added to their name."); | ||
| Console.WriteLine ("DLL files from AAB/APK archives are uncompressed to a subdirectory of the current directory named after the archive with extension removed"); | ||
| return 1; | ||
| } | ||
|
|
||
| static bool UncompressDLL (Stream inputStream, string fileName, string filePath, string prefix) | ||
| { | ||
| string outputFile = $"{prefix}{Path.GetFileName (filePath)}"; | ||
| bool retVal = true; | ||
|
|
||
| Console.WriteLine ($"Processing {fileName}"); | ||
| // | ||
| // LZ4 compressed assembly header format: | ||
| // uint magic; // 0x5A4C4158; 'XALZ', little-endian | ||
| // uint descriptor_index; // Index into an internal assembly descriptor table | ||
| // uint uncompressed_length; // Size of assembly, uncompressed | ||
| // | ||
| using (var reader = new BinaryReader (inputStream)) { | ||
| uint magic = reader.ReadUInt32 (); | ||
| if (magic == CompressedDataMagic) { | ||
| reader.ReadUInt32 (); // descriptor index, ignore | ||
| uint decompressedLength = reader.ReadUInt32 (); | ||
|
|
||
| int inputLength = (int)(inputStream.Length - 12); | ||
| byte[] sourceBytes = bytePool.Rent (inputLength); | ||
| reader.Read (sourceBytes, 0, inputLength); | ||
|
|
||
| byte[] assemblyBytes = bytePool.Rent ((int)decompressedLength); | ||
| int decoded = LZ4Codec.Decode (sourceBytes, 0, inputLength, assemblyBytes, 0, (int)decompressedLength); | ||
| if (decoded != (int)decompressedLength) { | ||
| Console.Error.WriteLine ($" Failed to decompress LZ4 data of {fileName} (decoded: {decoded})"); | ||
| retVal = false; | ||
| } else { | ||
| string outputDir = Path.GetDirectoryName (outputFile); | ||
| if (!String.IsNullOrEmpty (outputDir)) { | ||
| Directory.CreateDirectory (outputDir); | ||
| } | ||
| using (var fs = File.Open (outputFile, FileMode.Create, FileAccess.Write)) { | ||
| fs.Write (assemblyBytes, 0, assemblyBytes.Length); | ||
| fs.Flush (); | ||
| } | ||
| Console.WriteLine ($" uncompressed to: {outputFile}"); | ||
| } | ||
|
|
||
| bytePool.Return (sourceBytes); | ||
| bytePool.Return (assemblyBytes); | ||
| } else { | ||
| Console.WriteLine ($" assembly is not compressed"); | ||
| } | ||
| } | ||
|
|
||
| return retVal; | ||
| } | ||
|
|
||
| static bool UncompressDLL (string filePath, string prefix) | ||
| { | ||
| using (var fs = File.Open (filePath, FileMode.Open, FileAccess.Read)) { | ||
| return UncompressDLL (fs, filePath, filePath, prefix); | ||
| } | ||
| } | ||
|
|
||
| static bool UncompressFromAPK (string filePath, string assembliesPath) | ||
| { | ||
| string prefix = $"uncompressed-{Path.GetFileNameWithoutExtension (filePath)}{Path.DirectorySeparatorChar}"; | ||
| using (ZipArchive apk = ZipArchive.Open (filePath, FileMode.Open)) { | ||
| foreach (ZipEntry entry in apk) { | ||
| if (!entry.FullName.StartsWith (assembliesPath, StringComparison.Ordinal)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!entry.FullName.EndsWith (".dll", StringComparison.Ordinal)) { | ||
| continue; | ||
| } | ||
|
|
||
| using (var stream = new MemoryStream ()) { | ||
| entry.Extract (stream); | ||
| stream.Seek (0, SeekOrigin.Begin); | ||
| UncompressDLL (stream, $"{filePath}!{entry.FullName}", entry.FullName, prefix); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static int Main (string[] args) | ||
| { | ||
| if (args.Length == 0) { | ||
| return Usage (); | ||
| } | ||
|
|
||
| bool haveErrors = false; | ||
| foreach (string file in args) { | ||
| string ext = Path.GetExtension (file); | ||
| if (String.Compare (".dll", ext, StringComparison.OrdinalIgnoreCase) == 0) { | ||
| if (!UncompressDLL (file, "uncompressed-")) { | ||
| haveErrors = true; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (String.Compare (".apk", ext, StringComparison.OrdinalIgnoreCase) == 0) { | ||
| if (!UncompressFromAPK (file, "assemblies/")) { | ||
| haveErrors = true; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (String.Compare (".aab", ext, StringComparison.OrdinalIgnoreCase) == 0) { | ||
| if (!UncompressFromAPK (file, "base/root/assemblies/")) { | ||
| haveErrors = true; | ||
| } | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| return haveErrors ? 1 : 0; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/bin/bash | ||
| truepath=$(readlink "$0" || echo "$0") | ||
| exec mono --debug "${truepath}.exe" "$@" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be handy to extend to handle
.aabfiles as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support for AAB added :)