commit 0e3e11e3878e6d38bcd715df2fec91748da0a753 Author: Knaapchen Date: Wed Aug 31 19:07:50 2022 +0200 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3363a6a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,96 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "fugg-fix-mapping" +version = "0.1.0" +dependencies = [ + "parity-wasm", + "serde", + "serde_json", +] + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "parity-wasm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be5e13c266502aadf83426d87d81a0f5d1ef45b8027f5a471c360abfe4bfae92" + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..23a5763 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "fugg-fix-mapping" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +parity-wasm = "0.42" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6713980 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,77 @@ +use std::io::Read; +use std::{io::Write, fs::File}; +use std::path::Path; +use std::fs::OpenOptions; +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +struct FunctionFile { + imports: HashMap, + exports: HashMap +} + +fn main() { + let directory = Path::new("../fugg-fetch/out/55"); + + let input = directory.join("client-original.wasm"); + let output = directory.join("client.wasm"); + let functions_file = directory.join("functions.json"); + + let mut module = parity_wasm::deserialize_file(input).unwrap(); + assert!(module.code_section().is_some()); + + let mut file = File::open(functions_file).unwrap(); + let mut data = String::new(); + file.read_to_string(&mut data).unwrap(); + + let functions: FunctionFile = serde_json::from_str(&data).expect("JSON was not well-formatted"); + + println!("Functions: {:?}", functions); + + const MODULE: &str = "env"; + + { + let import_section = module.import_section_mut().unwrap(); + + for entry in import_section.entries_mut() { + let nice_name = functions.imports.get(entry.field()); + + *entry.module_mut() = String::from(MODULE); + if let Some(nice_name) = nice_name { + println!("Import: Renaming {}::{} to {}::{}", entry.module(), entry.field(), MODULE, nice_name); + + *entry.field_mut() = String::from(nice_name); + } else { + println!("Import: Unable to find nice name for {}::{}", entry.module(), entry.field()); + } + } + } + + { + let export_section = module.export_section_mut().unwrap(); + + for entry in export_section.entries_mut() { + let nice_name = functions.exports.get(entry.field()); + + if let Some(nice_name) = nice_name { + println!("Export: Renaming {} to {}", entry.field(), nice_name); + + *entry.field_mut() = String::from(nice_name); + } else { + println!("Export: Unable to find nice name for {}", entry.field()); + } + } + } + + let output_bytes = parity_wasm::serialize(module).unwrap(); + let mut file = OpenOptions::new() + .write(true) + .create(true) + .open(output).unwrap(); + + file.write_all(&output_bytes).unwrap(); + + +}