Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add cleanup_mapping_file function #38

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod stacktrace;

pub use mapper::{DeobfuscatedSignature, ProguardMapper, RemappedFrameIter};
pub use mapping::{
LineMapping, MappingSummary, ParseError, ParseErrorKind, ProguardMapping, ProguardRecord,
ProguardRecordIter,
cleanup_mapping_file, LineMapping, MappingSummary, ParseError, ParseErrorKind, ProguardMapping,
ProguardRecord, ProguardRecordIter,
};
pub use stacktrace::{StackFrame, StackTrace, Throwable};
85 changes: 75 additions & 10 deletions src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! [here](https://www.guardsquare.com/en/products/proguard/manual/retrace).

use std::fmt;
use std::io::{BufRead, Write};
use std::str;

#[cfg(feature = "uuid")]
Expand Down Expand Up @@ -206,16 +207,15 @@ impl<'s> ProguardMapping<'s> {
/// assert_eq!(without.has_line_info(), false);
/// ```
pub fn has_line_info(&self) -> bool {
// We are matching on the inner `ProguardRecord` anyway
#[allow(clippy::manual_flatten)]
for record in self.iter() {
if let Ok(ProguardRecord::Method { line_mapping, .. }) = record {
if line_mapping.is_some() {
return true;
}
}
}
false
self.iter().any(|record| {
matches!(
record,
Ok(ProguardRecord::Method {
line_mapping: Some(_),
..
})
)
})
}

/// Calculates the UUID of the mapping file.
Expand Down Expand Up @@ -690,8 +690,45 @@ fn is_newline(byte: &u8) -> bool {
*byte == b'\r' || *byte == b'\n'
}

/// Cleans up a Proguard mapping file by removing all records
/// that can't be used by a [`ProguardMapper`].
///
/// This preserves class, method, and "sourceFile" header records.
/// It removes field and all other header records, as well as all invalid lines.
pub fn cleanup_mapping_file<R, W>(mut source: R, mut output: W) -> Result<(), std::io::Error>
where
R: BufRead,
W: Write,
{
let mut line_buf = String::new();
loop {
line_buf.clear();
let read = source.read_line(&mut line_buf)?;
loewenheim marked this conversation as resolved.
Show resolved Hide resolved

if read == 0 {
break;
}

let (record, _rest) = parse_proguard_record(line_buf.as_bytes());
if matches!(
record,
Ok(ProguardRecord::Class { .. }
| ProguardRecord::Method { .. }
| ProguardRecord::Header {
key: "sourceFile",
..
})
) {
output.write_all(line_buf.as_bytes())?;
}
}
Ok(())
}

#[cfg(test)]
mod tests {
use std::io::Cursor;

use super::*;

#[test]
Expand Down Expand Up @@ -1060,4 +1097,32 @@ androidx.activity.OnBackPressedCallback
],
);
}

#[test]
fn cleanup() {
let bytes = br#"
# compiler: R8
# common_typos_disable

androidx.activity.OnBackPressedCallback->c.a.b:
androidx.activity.OnBackPressedCallback -> c.a.b:
# {"id":"sourceFile","fileName":"Test.kt"}
boolean mEnabled -> a
boolean mEnabled -> a
java.util.ArrayDeque mOnBackPressedCallbacks -> b
1:4:void onBackPressed():184:187 -> c
androidx.activity.OnBackPressedCallback
-> c.a.b:
"#;

let mut out = Vec::new();

cleanup_mapping_file(&mut Cursor::new(bytes), &mut out).unwrap();

let expected = br#"androidx.activity.OnBackPressedCallback -> c.a.b:
# {"id":"sourceFile","fileName":"Test.kt"}
1:4:void onBackPressed():184:187 -> c
"#;
assert_eq!(out, expected);
}
}
Loading