|
| 1 | +use crate::extractors::common::{Chroot, ExtractionResult, Extractor, ExtractorType}; |
| 2 | + |
| 3 | +/// Defines the internal extractor function for u16 swapped firmware images |
| 4 | +/// |
| 5 | +/// ``` |
| 6 | +/// use std::io::ErrorKind; |
| 7 | +/// use std::process::Command; |
| 8 | +/// use binwalk::extractors::common::ExtractorType; |
| 9 | +/// use binwalk::extractors::swapped::swapped_extractor_u16; |
| 10 | +/// |
| 11 | +/// match swapped_extractor_u16().utility { |
| 12 | +/// ExtractorType::None => panic!("Invalid extractor type of None"), |
| 13 | +/// ExtractorType::Internal(func) => println!("Internal extractor OK: {:?}", func), |
| 14 | +/// ExtractorType::External(cmd) => { |
| 15 | +/// if let Err(e) = Command::new(&cmd).output() { |
| 16 | +/// if e.kind() == ErrorKind::NotFound { |
| 17 | +/// panic!("External extractor '{}' not found", cmd); |
| 18 | +/// } else { |
| 19 | +/// panic!("Failed to execute external extractor '{}': {}", cmd, e); |
| 20 | +/// } |
| 21 | +/// } |
| 22 | +/// } |
| 23 | +/// } |
| 24 | +/// ``` |
| 25 | +pub fn swapped_extractor_u16() -> Extractor { |
| 26 | + Extractor { |
| 27 | + utility: ExtractorType::Internal(extract_swapped_u16), |
| 28 | + ..Default::default() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Extract firmware where every two bytes have been swapped |
| 33 | +pub fn extract_swapped_u16( |
| 34 | + file_data: &[u8], |
| 35 | + offset: usize, |
| 36 | + output_directory: Option<&String>, |
| 37 | +) -> ExtractionResult { |
| 38 | + const SWAP_BYTE_COUNT: usize = 2; |
| 39 | + extract_swapped(file_data, offset, output_directory, SWAP_BYTE_COUNT) |
| 40 | +} |
| 41 | + |
| 42 | +/// Extract a block of data where every n bytes have been swapped |
| 43 | +fn extract_swapped( |
| 44 | + file_data: &[u8], |
| 45 | + offset: usize, |
| 46 | + output_directory: Option<&String>, |
| 47 | + n: usize, |
| 48 | +) -> ExtractionResult { |
| 49 | + const OUTPUT_FILE_NAME: &str = "swapped.bin"; |
| 50 | + |
| 51 | + let mut result = ExtractionResult { |
| 52 | + ..Default::default() |
| 53 | + }; |
| 54 | + |
| 55 | + if let Some(data) = file_data.get(offset..) { |
| 56 | + let swapped_data = byte_swap(data, n); |
| 57 | + |
| 58 | + result.success = !swapped_data.is_empty(); |
| 59 | + |
| 60 | + if result.success { |
| 61 | + result.size = Some(swapped_data.len()); |
| 62 | + |
| 63 | + // Write to file, if requested |
| 64 | + if output_directory.is_some() { |
| 65 | + let chroot = Chroot::new(output_directory); |
| 66 | + result.success = chroot.create_file(OUTPUT_FILE_NAME, &swapped_data); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + result |
| 72 | +} |
| 73 | + |
| 74 | +/// Swap every n bytes of the provided data |
| 75 | +/// |
| 76 | +/// ## Example: |
| 77 | +/// |
| 78 | +/// ``` |
| 79 | +/// use binwalk::extractors::swapped::byte_swap; |
| 80 | +/// |
| 81 | +/// assert_eq!(byte_swap(b"ABCD", 2), b"CDAB"); |
| 82 | +/// ``` |
| 83 | +pub fn byte_swap(data: &[u8], n: usize) -> Vec<u8> { |
| 84 | + let chunk_size = n * 2; |
| 85 | + let mut chunker = data.chunks(chunk_size); |
| 86 | + let mut swapped_data: Vec<u8> = Vec::new(); |
| 87 | + |
| 88 | + loop { |
| 89 | + match chunker.next() { |
| 90 | + None => { |
| 91 | + break; |
| 92 | + } |
| 93 | + Some(chunk) => { |
| 94 | + if chunk.len() != chunk_size { |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + swapped_data.extend(chunk[n..].to_vec()); |
| 99 | + swapped_data.extend(chunk[0..n].to_vec()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + swapped_data |
| 105 | +} |
0 commit comments