-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from sunnamed434/feature/costura-fody-support
Implement feature for costura fody resources as references
- Loading branch information
Showing
20 changed files
with
178 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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 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,4 @@ | ||
Does BitMono provides Costura-Fody Support? | ||
=========================================== | ||
|
||
Indeed, by default BitMono provides support for the Costura-Fody, resources will be resolved automatically, in this case, you don't need to have copies of your .DLLs or even set ``DisableCleanup`` to ``true`` in ``FodyWeavers.xml``. |
This file contains 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 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 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 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 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 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
23 changes: 23 additions & 0 deletions
23
src/BitMono.Obfuscation/Abstractions/AutomaticReferencesDataResolver.cs
This file contains 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,23 @@ | ||
namespace BitMono.Obfuscation.Abstractions; | ||
|
||
public class AutomaticReferencesDataResolver : IReferencesDataResolver | ||
{ | ||
private readonly string _referencesDirectoryName; | ||
|
||
public AutomaticReferencesDataResolver(string referencesDirectoryName) | ||
{ | ||
_referencesDirectoryName = referencesDirectoryName; | ||
} | ||
|
||
[SuppressMessage("ReSharper", "ConvertIfStatementToReturnStatement")] | ||
public IEnumerable<byte[]> Resolve(ModuleDefinition module) | ||
{ | ||
var referencesData = new ReferencesDataResolver(_referencesDirectoryName).Resolve(module); | ||
var costuraReferencesData = new CosturaReferencesDataResolver().Resolve(module); | ||
if (costuraReferencesData.IsEmpty() == false) | ||
{ | ||
return referencesData.Concat(costuraReferencesData); | ||
} | ||
return referencesData; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/BitMono.Obfuscation/Abstractions/CosturaReferencesDataResolver.cs
This file contains 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,47 @@ | ||
namespace BitMono.Obfuscation.Abstractions; | ||
|
||
public class CosturaReferencesDataResolver : IReferencesDataResolver | ||
{ | ||
private const string CosturaResourceNameStart = "costura."; | ||
private const string CosturaResourceNameEnd = ".dll.compressed"; | ||
private const int MinCosturaResourceNameCharactersLenght = 19; | ||
|
||
[SuppressMessage("ReSharper", "ForCanBeConvertedToForeach")] | ||
[SuppressMessage("ReSharper", "LoopCanBeConvertedToQuery")] | ||
[SuppressMessage("ReSharper", "InvertIf")] | ||
public IEnumerable<byte[]> Resolve(ModuleDefinition module) | ||
{ | ||
for (var i = 0; i < module.Resources.Count; i++) | ||
{ | ||
var resource = module.Resources[i]; | ||
if (resource.IsEmbedded) | ||
{ | ||
if (Utf8String.IsNullOrEmpty(resource.Name) == false) | ||
{ | ||
var name = resource.Name.Value; | ||
if (name.Length > MinCosturaResourceNameCharactersLenght) | ||
{ | ||
if (name.StartsWith(CosturaResourceNameStart) && name.EndsWith(CosturaResourceNameEnd)) | ||
{ | ||
var data = resource.GetData(); | ||
if (data != null) | ||
{ | ||
data = Decompress(data); | ||
yield return data; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static byte[] Decompress(byte[] data) | ||
{ | ||
using var input = new MemoryStream(data); | ||
using var output = new MemoryStream(); | ||
using var deflateStream = new DeflateStream(input, CompressionMode.Decompress); | ||
deflateStream.CopyTo(output); | ||
return output.ToArray(); | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
src/BitMono.Obfuscation/Abstractions/DependenciesDataResolver.cs
This file was deleted.
Oops, something went wrong.
This file contains 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
21 changes: 21 additions & 0 deletions
21
src/BitMono.Obfuscation/Abstractions/ReferencesDataResolver.cs
This file contains 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,21 @@ | ||
namespace BitMono.Obfuscation.Abstractions; | ||
|
||
public class ReferencesDataResolver : IReferencesDataResolver | ||
{ | ||
private readonly string _referencesDirectoryName; | ||
|
||
public ReferencesDataResolver(string referencesDirectoryName) | ||
{ | ||
_referencesDirectoryName = referencesDirectoryName; | ||
} | ||
|
||
[SuppressMessage("ReSharper", "ForCanBeConvertedToForeach")] | ||
public IEnumerable<byte[]> Resolve(ModuleDefinition module) | ||
{ | ||
var dependencies = Directory.GetFiles(_referencesDirectoryName); | ||
for (var i = 0; i < dependencies.Length; i++) | ||
{ | ||
yield return File.ReadAllBytes(dependencies[i]); | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.