Skip to content

Commit

Permalink
Merge pull request mozilla#10 from calixteman/cab_input
Browse files Browse the repository at this point in the history
Allow pdb in cabinet archive as input
  • Loading branch information
calixteman committed Oct 1, 2019
2 parents bd814b6 + 9490b4a commit 1aa8a8a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"
license = "MIT/Apache-2.0"

[dependencies]
cab = "0.2"
clap = "2.33"
failure = "0.1"
fxhash = "0.2"
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
.about("Dump debug symbols to breakpad symbols")
.arg(
Arg::with_name("filename")
.help("File to dump")
.help("File to dump (.dll, .exe, .pdb or .pd_)")
.required(true)
.takes_value(true),
)
Expand Down Expand Up @@ -70,7 +70,7 @@ fn main() {
Err(From::from("No pdb file found"))
}
}
"pdb" => {
"pdb" | "pd_" => {
let output = get_writer_for_sym(&output);
windows::pdb::PDBInfo::dump(&buf, filename, "".to_string(), None, output)
}
Expand Down
56 changes: 53 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use cab::Cabinet;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::io::{Cursor, Read};
use std::path::{Path, PathBuf};

pub fn read_file<P: AsRef<Path>>(path: P) -> Vec<u8> {
let mut file = File::open(&path).unwrap_or_else(|_| {
Expand All @@ -22,5 +23,54 @@ pub fn read_file<P: AsRef<Path>>(path: P) -> Vec<u8> {
)
});

buf
read_cabinet(buf, path.as_ref().to_path_buf()).unwrap_or_else(|| {
panic!(
"Unable to read the file {}",
path.as_ref().to_str().unwrap()
)
})
}

fn read_cabinet(buf: Vec<u8>, path: PathBuf) -> Option<Vec<u8>> {
// try to find a pdb in cabinet archive
// if not a cabinet just return the buffer
// else return None on error

let cursor = Cursor::new(&buf);
let mut cab = match Cabinet::new(cursor) {
Ok(cab) => cab,
_ => return Some(buf),
};

let file = match get_cabinet_files(&cab, path) {
Some(file) => file,
_ => return None,
};

let mut buf = Vec::new();
let mut reader = match cab.read_file(&file) {
Ok(reader) => reader,
_ => return None,
};

if reader.read_to_end(&mut buf).is_err() {
None
} else {
Some(buf)
}
}

fn get_cabinet_files<'a>(cab: &'a Cabinet<Cursor<&Vec<u8>>>, path: PathBuf) -> Option<String> {
// Try to find in the cabinet the same path with pdb extension
let path = path.with_extension("pdb");
let file_name = path.file_name().unwrap();
for folder in cab.folder_entries() {
for file in folder.file_entries() {
let path = PathBuf::from(file.name());
if path.file_name().unwrap() == file_name {
return Some(file.name().to_string());
}
}
}
None
}
21 changes: 13 additions & 8 deletions src/windows/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ pub fn get_win_path(path: &str) -> PathBuf {
fn try_to_find_pdb(path: PathBuf, pdb_filename: &str) -> Option<Vec<u8>> {
// Just check that the file is in the same directory as the PE one
let pdb = path.with_file_name(pdb_filename);
if pdb.is_file() {
Some(utils::read_file(pdb))
} else {
// We try in CWD
let mut pdb = std::env::current_dir().expect("Unable to get the current working directory");
pdb.set_file_name(pdb_filename);
let mut pdb_cab = pdb.clone();
pdb_cab.set_extension("pd_");

for pdb in vec![pdb, pdb_cab].drain(..) {
if pdb.is_file() {
Some(utils::read_file(pdb))
return Some(utils::read_file(pdb));
} else {
None
// We try in CWD
let mut pdb =
std::env::current_dir().expect("Unable to get the current working directory");
pdb.set_file_name(pdb_filename);
if pdb.is_file() {
return Some(utils::read_file(pdb));
}
}
}
None
}

#[cfg(windows)]
Expand Down
Binary file renamed test_data/basic32.pdb → test_data/basic32.pd_
Binary file not shown.

0 comments on commit 1aa8a8a

Please sign in to comment.