use crate::{
cilassembly::CleanupRequest,
deobfuscation::{
cleanup::is_entry_point, context::AnalysisContext, findings::DeobfuscationFindings,
},
CilObject,
};
pub fn build_request(
assembly: &CilObject,
ctx: &AnalysisContext,
findings: &DeobfuscationFindings,
) -> Option<CleanupRequest> {
let cleanup_config = &ctx.config.cleanup;
if !cleanup_config.any_enabled() {
return None;
}
let request = build_cleanup_request(findings, assembly, ctx);
if request.has_deletions() {
Some(request)
} else {
None
}
}
fn build_cleanup_request(
findings: &DeobfuscationFindings,
assembly: &CilObject,
ctx: &AnalysisContext,
) -> CleanupRequest {
let mut request = CleanupRequest::with_settings(
ctx.config.cleanup.remove_orphan_metadata,
ctx.config.cleanup.remove_empty_types,
);
let aggressive = ctx.config.cleanup.remove_unused_methods;
if ctx.config.cleanup.remove_protection_methods {
for (_, type_token) in &findings.protection_infrastructure_types {
request.add_type(*type_token);
}
}
if let Some(token) = findings.suppress_ildasm_token {
request.add_attribute(token);
}
if ctx.config.cleanup.remove_decryptors {
for token in ctx.decryptors.removable_decryptors() {
if !is_entry_point(assembly, token, aggressive) {
request.add_method(token);
}
}
}
if ctx.config.cleanup.remove_protection_methods {
for (_, token) in &findings.decryptor_methods {
if !is_entry_point(assembly, *token, aggressive) {
request.add_method(*token);
}
}
}
if ctx.config.cleanup.remove_protection_methods {
for (_, field_token) in &findings.infrastructure_fields {
request.add_field(*field_token);
}
}
request
}