| 1 |
use crate::add_metadata::AddMetadataField; |
| 2 |
use crate::{AddMetadata, ComponentNames, ModuleNames, Producers}; |
| 3 |
use anyhow::Result; |
| 4 |
use std::mem; |
| 5 |
use wasm_encoder::ComponentSection as _; |
| 6 |
use wasm_encoder::{ComponentSectionId, Encode, Section}; |
| 7 |
use wasmparser::{KnownCustom, Parser, Payload::*}; |
| 8 |
|
| 9 |
pub(crate) fn rewrite_wasm( |
| 10 |
metadata: &AddMetadata, |
| 11 |
add_producers: &Producers, |
| 12 |
input: &[u8], |
| 13 |
) -> Result<Vec<u8>> { |
| 14 |
let mut producers_found = false; |
| 15 |
let mut names_found = false; |
| 16 |
let mut stack = Vec::new(); |
| 17 |
let mut output = Vec::new(); |
| 18 |
for payload in Parser::new(0).parse_all(&input) { |
| 19 |
let payload = payload?; |
| 20 |
|
| 21 |
// Track nesting depth, so that we don't mess with inner producer sections: |
| 22 |
match payload { |
| 23 |
Version { encoding, .. } => { |
| 24 |
output.extend_from_slice(match encoding { |
| 25 |
wasmparser::Encoding::Component => &wasm_encoder::Component::HEADER, |
| 26 |
wasmparser::Encoding::Module => &wasm_encoder::Module::HEADER, |
| 27 |
}); |
| 28 |
} |
| 29 |
ModuleSection { .. } | ComponentSection { .. } => { |
| 30 |
stack.push(mem::take(&mut output)); |
| 31 |
continue; |
| 32 |
} |
| 33 |
End { .. } => { |
| 34 |
let mut parent = match stack.pop() { |
| 35 |
Some(c) => c, |
| 36 |
None => break, |
| 37 |
}; |
| 38 |
if output.starts_with(&wasm_encoder::Component::HEADER) { |
| 39 |
parent.push(ComponentSectionId::Component as u8); |
| 40 |
output.encode(&mut parent); |
| 41 |
} else { |
| 42 |
parent.push(ComponentSectionId::CoreModule as u8); |
| 43 |
output.encode(&mut parent); |
| 44 |
} |
| 45 |
output = parent; |
| 46 |
} |
| 47 |
_ => {} |
| 48 |
} |
| 49 |
|
| 50 |
// Only rewrite the outermost custom sections |
| 51 |
if let CustomSection(c) = &payload { |
| 52 |
if stack.len() == 0 { |
| 53 |
match c.as_known() { |
| 54 |
KnownCustom::Producers(_) => { |
| 55 |
producers_found = true; |
| 56 |
let mut producers = Producers::from_bytes(c.data(), c.data_offset())?; |
| 57 |
// Add to the section according to the command line flags: |
| 58 |
producers.merge(&add_producers); |
| 59 |
// Encode into output: |
| 60 |
producers.section().append_to(&mut output); |
| 61 |
continue; |
| 62 |
} |
| 63 |
KnownCustom::Name(_) => { |
| 64 |
names_found = true; |
| 65 |
let mut names = ModuleNames::from_bytes(c.data(), c.data_offset())?; |
| 66 |
match &metadata.name { |
| 67 |
AddMetadataField::Keep => {} |
| 68 |
AddMetadataField::Set(name) => { |
| 69 |
names.merge(&ModuleNames::from_name(&Some(name.clone()))); |
| 70 |
} |
| 71 |
AddMetadataField::Clear => { |
| 72 |
names = ModuleNames::empty(); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
names.section()?.as_custom().append_to(&mut output); |
| 77 |
continue; |
| 78 |
} |
| 79 |
KnownCustom::ComponentName(_) => { |
| 80 |
names_found = true; |
| 81 |
let mut names = ComponentNames::from_bytes(c.data(), c.data_offset())?; |
| 82 |
match &metadata.name { |
| 83 |
AddMetadataField::Keep => {} |
| 84 |
AddMetadataField::Set(name) => { |
| 85 |
names.merge(&ComponentNames::from_name(&Some(name.clone()))); |
| 86 |
} |
| 87 |
AddMetadataField::Clear => { |
| 88 |
names = ComponentNames::empty(); |
| 89 |
} |
| 90 |
}; |
| 91 |
names.section()?.as_custom().append_to(&mut output); |
| 92 |
continue; |
| 93 |
} |
| 94 |
#[cfg(feature = "oci")] |
| 95 |
KnownCustom::Unknown if c.name() == "author" => { |
| 96 |
if metadata.authors.is_keep() { |
| 97 |
let author = crate::Authors::parse_custom_section(c)?; |
| 98 |
author.append_to(&mut output); |
| 99 |
continue; |
| 100 |
} else if metadata.authors.is_clear() { |
| 101 |
continue; |
| 102 |
} |
| 103 |
} |
| 104 |
#[cfg(feature = "oci")] |
| 105 |
KnownCustom::Unknown if c.name() == "description" => { |
| 106 |
if metadata.description.is_keep() { |
| 107 |
let description = crate::Description::parse_custom_section(c)?; |
| 108 |
description.append_to(&mut output); |
| 109 |
continue; |
| 110 |
} else if metadata.description.is_clear() { |
| 111 |
continue; |
| 112 |
} |
| 113 |
} |
| 114 |
#[cfg(feature = "oci")] |
| 115 |
KnownCustom::Unknown if c.name() == "licenses" => { |
| 116 |
if metadata.licenses.is_keep() { |
| 117 |
let licenses = crate::Licenses::parse_custom_section(c)?; |
| 118 |
licenses.append_to(&mut output); |
| 119 |
continue; |
| 120 |
} else if metadata.licenses.is_clear() { |
| 121 |
continue; |
| 122 |
} |
| 123 |
} |
| 124 |
#[cfg(feature = "oci")] |
| 125 |
KnownCustom::Unknown if c.name() == "source" => { |
| 126 |
if metadata.source.is_keep() { |
| 127 |
let source = crate::Source::parse_custom_section(c)?; |
| 128 |
source.append_to(&mut output); |
| 129 |
continue; |
| 130 |
} else if metadata.source.is_clear() { |
| 131 |
continue; |
| 132 |
} |
| 133 |
} |
| 134 |
#[cfg(feature = "oci")] |
| 135 |
KnownCustom::Unknown if c.name() == "homepage" => { |
| 136 |
if metadata.homepage.is_keep() { |
| 137 |
let homepage = crate::Homepage::parse_custom_section(c)?; |
| 138 |
homepage.append_to(&mut output); |
| 139 |
continue; |
| 140 |
} else if metadata.homepage.is_clear() { |
| 141 |
continue; |
| 142 |
} |
| 143 |
} |
| 144 |
#[cfg(feature = "oci")] |
| 145 |
KnownCustom::Unknown if c.name() == "revision" => { |
| 146 |
if metadata.revision.is_keep() { |
| 147 |
let revision = crate::Revision::parse_custom_section(c)?; |
| 148 |
revision.append_to(&mut output); |
| 149 |
continue; |
| 150 |
} else if metadata.revision.is_clear() { |
| 151 |
continue; |
| 152 |
} |
| 153 |
} |
| 154 |
#[cfg(feature = "oci")] |
| 155 |
KnownCustom::Unknown if c.name() == "version" => { |
| 156 |
if metadata.version.is_keep() { |
| 157 |
let version = crate::Version::parse_custom_section(c)?; |
| 158 |
version.append_to(&mut output); |
| 159 |
continue; |
| 160 |
} else if metadata.version.is_clear() { |
| 161 |
continue; |
| 162 |
} |
| 163 |
} |
| 164 |
_ => {} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
// All other sections get passed through unmodified: |
| 169 |
if let Some((id, range)) = payload.as_section() { |
| 170 |
wasm_encoder::RawSection { |
| 171 |
id, |
| 172 |
data: &input[range], |
| 173 |
} |
| 174 |
.append_to(&mut output); |
| 175 |
} |
| 176 |
} |
| 177 |
if !names_found { |
| 178 |
if let AddMetadataField::Set(name) = &metadata.name { |
| 179 |
if output.starts_with(&wasm_encoder::Component::HEADER) { |
| 180 |
let names = ComponentNames::from_name(&Some(name.clone())); |
| 181 |
names.section()?.append_to_component(&mut output); |
| 182 |
} else { |
| 183 |
let names = ModuleNames::from_name(&Some(name.clone())); |
| 184 |
names.section()?.append_to(&mut output) |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
if !producers_found && !add_producers.is_empty() { |
| 189 |
let mut producers = Producers::empty(); |
| 190 |
// Add to the section according to the command line flags: |
| 191 |
producers.merge(add_producers); |
| 192 |
// Encode into output: |
| 193 |
producers.section().append_to(&mut output); |
| 194 |
} |
| 195 |
#[cfg(feature = "oci")] |
| 196 |
if let AddMetadataField::Set(author) = &metadata.authors { |
| 197 |
author.append_to(&mut output); |
| 198 |
} |
| 199 |
#[cfg(feature = "oci")] |
| 200 |
if let AddMetadataField::Set(description) = &metadata.description { |
| 201 |
description.append_to(&mut output); |
| 202 |
} |
| 203 |
#[cfg(feature = "oci")] |
| 204 |
if let AddMetadataField::Set(licenses) = &metadata.licenses { |
| 205 |
licenses.append_to(&mut output); |
| 206 |
} |
| 207 |
#[cfg(feature = "oci")] |
| 208 |
if let AddMetadataField::Set(source) = &metadata.source { |
| 209 |
source.append_to(&mut output); |
| 210 |
} |
| 211 |
#[cfg(feature = "oci")] |
| 212 |
if let AddMetadataField::Set(homepage) = &metadata.homepage { |
| 213 |
homepage.append_to(&mut output); |
| 214 |
} |
| 215 |
#[cfg(feature = "oci")] |
| 216 |
if let AddMetadataField::Set(revision) = &metadata.revision { |
| 217 |
revision.append_to(&mut output); |
| 218 |
} |
| 219 |
#[cfg(feature = "oci")] |
| 220 |
if let AddMetadataField::Set(version) = &metadata.version { |
| 221 |
version.append_to(&mut output); |
| 222 |
} |
| 223 |
Ok(output) |
| 224 |
} |