use crate::signature::*;
use crate::wasm_module::*;
use log::*;
impl Module {
pub fn show(&self, verbose: bool) -> Result<(), WSError> {
for (idx, section) in self.sections.iter().enumerate() {
println!("{}:\t{}", idx, section.display(verbose));
}
Ok(())
}
pub fn split<P>(self, mut predicate: P) -> Result<Module, WSError>
where
P: FnMut(&Section) -> bool,
{
let mut out_sections = vec![];
let mut flip = false;
let mut last_was_delimiter = false;
for (idx, section) in self.sections.into_iter().enumerate() {
if section.is_signature_header() {
info!("Module is already signed");
out_sections.push(section);
continue;
}
if section.is_signature_delimiter() {
out_sections.push(section);
last_was_delimiter = true;
continue;
}
let section_can_be_signed = predicate(§ion);
if idx == 0 {
flip = !section_can_be_signed;
} else if section_can_be_signed == flip {
if !last_was_delimiter {
let delimiter = new_delimiter_section()?;
out_sections.push(delimiter);
}
flip = !flip;
}
out_sections.push(section);
last_was_delimiter = false;
}
if let Some(last_section) = out_sections.last() {
if !last_section.is_signature_delimiter() {
let delimiter = new_delimiter_section()?;
out_sections.push(delimiter);
}
}
Ok(Module {
header: self.header,
sections: out_sections,
})
}
pub fn detach_signature(mut self) -> Result<(Module, Vec<u8>), WSError> {
if self.sections.is_empty() {
return Err(WSError::NoSignatures);
}
let first_section = self.sections.remove(0);
if !first_section.is_signature_header() {
return Err(WSError::NoSignatures);
}
let detached_signature = first_section.payload().to_vec();
debug!("Signature detached");
Ok((self, detached_signature))
}
pub fn attach_signature(mut self, detached_signature: &[u8]) -> Result<Module, WSError> {
for section in &self.sections {
if section.is_signature_header() {
return Err(WSError::SignatureAlreadyAttached);
}
}
let signature_header = Section::Custom(CustomSection::new(
SIGNATURE_SECTION_HEADER_NAME.to_string(),
detached_signature.to_vec(),
));
self.sections.insert(0, signature_header);
debug!("Signature attached");
Ok(self)
}
}