Performs dynamic modifications of `input` based on environment variables
(input: &mut MultiStream)
| 77 | |
| 78 | /// Performs dynamic modifications of `input` based on environment variables |
| 79 | pub fn modify_input(input: &mut MultiStream) { |
| 80 | let mut modified = false; |
| 81 | |
| 82 | if let Ok(trim) = std::env::var("TRIM") { |
| 83 | modified = true; |
| 84 | if let Some((stream, offset, size)) = parse_trim(&trim) { |
| 85 | input.streams.get_mut(&stream).unwrap().bytes.drain(offset..offset + size); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if let Ok(insert) = std::env::var("INSERT") { |
| 90 | modified = true; |
| 91 | for insert in insert.split(';') { |
| 92 | if let Some((stream, offset, bytes)) = parse_modification(insert) { |
| 93 | utils::insert_slice( |
| 94 | &mut input.streams.get_mut(&stream).unwrap().bytes, |
| 95 | &bytes, |
| 96 | offset, |
| 97 | ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if let Ok(insert) = std::env::var("REPLACE") { |
| 103 | modified = true; |
| 104 | for insert in insert.split(';') { |
| 105 | if let Some((stream, offset, bytes)) = parse_modification(insert) { |
| 106 | tracing::info!("{stream:#x}@{offset} replacing {} bytes", bytes.len()); |
| 107 | let dst = &mut input.streams.get_mut(&stream).unwrap().bytes; |
| 108 | dst[offset..offset + bytes.len()].copy_from_slice(&bytes); |
| 109 | } |
| 110 | else { |
| 111 | tracing::warn!("invalid modification: {insert}"); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if modified { |
| 117 | // Save a copy of the modified version of the input for debugging and analysis. |
| 118 | let _ = std::fs::write("workdir/modified_input", input.to_bytes()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// Parses a trim expression of the form: "<stream addr>,<stream offset>,<size>" |
| 123 | fn parse_trim(trim: &str) -> Option<(u64, usize, usize)> { |
no test coverage detected