Skip to content

Commit

Permalink
refactor: PeEmbeddedPortablePDB decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Feb 7, 2023
1 parent 20bd5d3 commit 630a347
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
31 changes: 6 additions & 25 deletions symbolic-debuginfo/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::BufWriter;
use std::io::Read;
use std::io::Write;

use gimli::RunTimeEndian;
use goblin::pe;
Expand Down Expand Up @@ -505,31 +503,14 @@ impl<'data, 'object> PeEmbeddedPortablePDB<'data> {
self.uncompressed_size
}

/// Reads the Portable PDB contents into the provided vector.
pub fn decompress_to_vec(&self) -> Result<Vec<u8>, PeError> {
let mut decoder = flate2::read::DeflateDecoder::new(self.compressed_data);
let mut output: Vec<u8> = vec![0; self.uncompressed_size];
let read_size = decoder.read(&mut output).map_err(PeError::new)?;
if read_size != self.uncompressed_size {
return Err(PeError::new(symbolic_ppdb::FormatError::from(
symbolic_ppdb::FormatErrorKind::InvalidLength,
)));
}
Ok(output)
}

/// Reads the Portable PDB contents into the provided file.
pub fn decompress_to_file(&self, output: File) -> Result<File, PeError> {
/// Reads the Portable PDB contents into the writer.
pub fn decompress_to<W: Write>(&self, output: W) -> Result<(), PeError> {
use std::io::prelude::*;
let buf_writer = BufWriter::new(output);
let mut decoder = flate2::write::DeflateDecoder::new(buf_writer);
let mut decoder = flate2::write::DeflateDecoder::new(output);
decoder
.write_all(self.compressed_data)
.map_err(PeError::new)?;
decoder
.finish()
.map_err(PeError::new)?
.into_inner()
.map_err(PeError::new)
decoder.finish().map_err(PeError::new)?;
Ok(())
}
}
7 changes: 4 additions & 3 deletions symbolic-debuginfo/tests/test_objects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, ffi::CString, fmt};
use std::{env, ffi::CString, fmt, io::BufWriter};

use symbolic_common::ByteView;
use symbolic_debuginfo::{
Expand Down Expand Up @@ -565,11 +565,12 @@ fn test_pe_embedded_ppdb() -> Result<(), Error> {
let embedded_ppdb = pe.embedded_ppdb().unwrap().unwrap();
assert_eq!(embedded_ppdb.get_size(), 10540);

let buf = embedded_ppdb.decompress_to_vec()?;
let mut buf = Vec::new();
embedded_ppdb.decompress_to(&mut buf)?;
assert_eq!(&buf[15..25], "\0PDB v1.0\0".as_bytes());

let tmp_file = tempfile::tempfile()?;
let tmp_file = embedded_ppdb.decompress_to_file(tmp_file)?;
embedded_ppdb.decompress_to(BufWriter::new(&tmp_file))?;
let file_buf = ByteView::map_file(tmp_file)?;
assert_eq!(buf, file_buf.as_slice());
}
Expand Down
6 changes: 4 additions & 2 deletions symbolic-ppdb/tests/test_ppdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ fn test_pe_embedded_ppdb_without_sources() {
let pe = PeObject::parse(&pe_buf).unwrap();

let embedded_ppdb = pe.embedded_ppdb().unwrap().unwrap();
let ppdb_buf = embedded_ppdb.decompress_to_vec().unwrap();
let mut ppdb_buf = Vec::new();
embedded_ppdb.decompress_to(&mut ppdb_buf).unwrap();
let ppdb = PortablePdb::parse(&ppdb_buf).unwrap();

assert_eq!(ppdb.pdb_id().unwrap(), pe.debug_id());
Expand All @@ -128,7 +129,8 @@ fn test_pe_embedded_ppdb_with_sources() {
let pe = PeObject::parse(&pe_buf).unwrap();

let embedded_ppdb = pe.embedded_ppdb().unwrap().unwrap();
let ppdb_buf = embedded_ppdb.decompress_to_vec().unwrap();
let mut ppdb_buf = Vec::new();
embedded_ppdb.decompress_to(&mut ppdb_buf).unwrap();
let ppdb = PortablePdb::parse(&ppdb_buf).unwrap();

assert_eq!(ppdb.pdb_id().unwrap(), pe.debug_id());
Expand Down

0 comments on commit 630a347

Please sign in to comment.