Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 12 additions & 9 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ uuid = ["dep:uuid", "lazy_static"]

[dependencies]
lazy_static = { version = "1.4.0", optional = true }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
thiserror = "1.0.61"
uuid = { version = "1.0.0", features = ["v5"], optional = true }
watto = { version = "0.1.0", features = ["writer", "strings"] }
Expand Down
2 changes: 1 addition & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct CacheError {
}

impl CacheError {
/// Returns the corresponding [`ErrorKind`] for this error.
/// Returns the corresponding [`CacheErrorKind`] for this error.
pub fn kind(&self) -> CacheErrorKind {
self.kind
}
Expand Down
13 changes: 5 additions & 8 deletions src/cache/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::Write;

use watto::{Pod, StringTable};

use crate::mapping::R8Header;
use crate::{ProguardMapping, ProguardRecord};

use super::{CacheError, CacheErrorKind};
Expand Down Expand Up @@ -194,15 +195,11 @@ impl<'data> ProguardCache<'data> {
let mut records = mapping.iter().filter_map(Result::ok).peekable();
while let Some(record) = records.next() {
match record {
ProguardRecord::Header {
key,
value: Some(file_name),
} => {
if key == "sourceFile" {
current_class.class.file_name_offset =
string_table.insert(file_name) as u32;
}
ProguardRecord::R8Header(R8Header::SourceFile { file_name }) => {
current_class.class.file_name_offset = string_table.insert(file_name) as u32;
}
ProguardRecord::Header { .. } => {}
ProguardRecord::R8Header(R8Header::Other) => {}
ProguardRecord::Class {
original,
obfuscated,
Expand Down
9 changes: 5 additions & 4 deletions src/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::{Error as FmtError, Write};
use std::iter::FusedIterator;

use crate::java;
use crate::mapping::R8Header;
use crate::mapping::{ProguardMapping, ProguardRecord};
use crate::stacktrace::{self, StackFrame, StackTrace, Throwable};

Expand Down Expand Up @@ -236,11 +237,11 @@ impl<'s> ProguardMapper<'s> {
let mut records = mapping.iter().filter_map(Result::ok).peekable();
while let Some(record) = records.next() {
match record {
ProguardRecord::Header { key, value } => {
if key == "sourceFile" {
class.file_name = value;
}
ProguardRecord::R8Header(R8Header::SourceFile { file_name }) => {
class.file_name = Some(file_name);
}
ProguardRecord::Header { .. } => {}
ProguardRecord::R8Header(R8Header::Other) => {}
ProguardRecord::Class {
original,
obfuscated,
Expand Down
80 changes: 50 additions & 30 deletions src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::fmt;
use std::ops::Range;
use std::str;

use serde::Deserialize;

#[cfg(feature = "uuid")]
use uuid::Uuid;

Expand Down Expand Up @@ -282,7 +284,7 @@ impl<'s> Iterator for ProguardRecordIter<'s> {
/// Maps start/end lines of a minified file to original start/end lines.
///
/// All line mappings are 1-based and inclusive.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LineMapping {
/// Start Line, 1-based.
pub startline: usize,
Expand All @@ -294,8 +296,26 @@ pub struct LineMapping {
pub original_endline: Option<usize>,
}

/// An R8 header, as described in
/// <https://r8.googlesource.com/r8/+/refs/heads/main/doc/retrace.md#additional-information-appended-as-comments-to-the-file>.
///
/// The format is a line starting with `#` and followed by a JSON object.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(tag = "id", rename_all = "camelCase")]
pub enum R8Header<'s> {
/// A source file header, stating what source file a class originated from.
///
/// See <https://r8.googlesource.com/r8/+/refs/heads/main/doc/retrace.md#source-file>.
#[serde(rename_all = "camelCase")]
SourceFile { file_name: &'s str },

/// Catchall variant for headers we don't support.
#[serde(other)]
Other,
}

/// A Proguard Mapping Record.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProguardRecord<'s> {
/// A Proguard Header.
Header {
Expand All @@ -304,6 +324,8 @@ pub enum ProguardRecord<'s> {
/// Optional value if the Header is a KV pair.
value: Option<&'s str>,
},
/// An R8 Header.
R8Header(R8Header<'s>),
/// A Class Mapping.
Class {
/// Original name of the class.
Expand Down Expand Up @@ -436,7 +458,9 @@ impl<'s> ProguardRecord<'s> {
fn parse_proguard_record(bytes: &[u8]) -> (Result<ProguardRecord, ParseError>, &[u8]) {
let bytes = consume_leading_newlines(bytes);

let result = if bytes.starts_with(b"#") {
let result = if bytes.starts_with(b"# {") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there always a space or is it optional in the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the spec isn't that detailed or explicit. In all examples I've seen there's a space, but it would probably make sense to be more careful here.

parse_r8_header(bytes)
} else if bytes.starts_with(b"#") {
parse_proguard_header(bytes)
} else if bytes.starts_with(b" ") {
parse_proguard_field_or_method(bytes)
Expand All @@ -459,38 +483,35 @@ fn parse_proguard_record(bytes: &[u8]) -> (Result<ProguardRecord, ParseError>, &
}
}

const SOURCE_FILE_PREFIX: &[u8; 32] = br#" {"id":"sourceFile","fileName":""#;

/// Parses a single Proguard Header from a Proguard File.
fn parse_proguard_header(bytes: &[u8]) -> Result<(ProguardRecord, &[u8]), ParseError> {
let bytes = parse_prefix(bytes, b"#")?;

if let Ok(bytes) = parse_prefix(bytes, SOURCE_FILE_PREFIX) {
let (value, bytes) = parse_until(bytes, |c| *c == b'"')?;
let bytes = parse_prefix(bytes, br#""}"#)?;
// Existing logic for `key: value` format
let (key, bytes) = parse_until(bytes, |c| *c == b':' || is_newline(c))?;

let record = ProguardRecord::Header {
key: "sourceFile",
value: Some(value),
};
let (value, bytes) = match parse_prefix(bytes, b":") {
Ok(bytes) => parse_until(bytes, is_newline).map(|(v, bytes)| (Some(v), bytes)),
Err(_) => Ok((None, bytes)),
}?;

Ok((record, consume_leading_newlines(bytes)))
} else {
// Existing logic for `key: value` format
let (key, bytes) = parse_until(bytes, |c| *c == b':' || is_newline(c))?;
let record = ProguardRecord::Header {
key: key.trim(),
value: value.map(|v| v.trim()),
};

let (value, bytes) = match parse_prefix(bytes, b":") {
Ok(bytes) => parse_until(bytes, is_newline).map(|(v, bytes)| (Some(v), bytes)),
Err(_) => Ok((None, bytes)),
}?;
Ok((record, consume_leading_newlines(bytes)))
}

let record = ProguardRecord::Header {
key: key.trim(),
value: value.map(|v| v.trim()),
};
fn parse_r8_header(bytes: &[u8]) -> Result<(ProguardRecord, &[u8]), ParseError> {
let bytes = parse_prefix(bytes, b"#")?;
let (header, rest) = parse_until(bytes, is_newline)?;

Ok((record, consume_leading_newlines(bytes)))
}
let header = serde_json::from_str(header).unwrap();
Ok((
ProguardRecord::R8Header(header),
consume_leading_newlines(rest),
))
}

/// Parses a single Proguard Field or Method from a Proguard File.
Expand Down Expand Up @@ -763,10 +784,9 @@ mod tests {
let parsed = ProguardRecord::try_parse(bytes);
assert_eq!(
parsed,
Ok(ProguardRecord::Header {
key: "sourceFile",
value: Some("Foobar.kt")
})
Ok(ProguardRecord::R8Header(R8Header::SourceFile {
file_name: "Foobar.kt",
}))
);
}

Expand Down
Loading