asc-engine 0.2.0

High-level asc-rs engine: parallel APK class lookup, cross-DEX reference search, single-class rebuild + disassembly, manifest decoding.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Class-name normalisation shared by the CLI and MCP, the readings of names
//! as decompiled Java / Kotlin source spells them ([`denormalize_query`]),
//! and the name mappings handed to decompiler backends ([`NameMappings`]).

use crate::{EngineError, Result};
use asc_findrefs::{ClassQuery, MemberQuery, MemberSignature, Query, TypeMatch};

mod denormalize;
mod mappings;
mod proguard;

pub use denormalize::{
    denormalize_class_candidates, denormalize_identifier, denormalize_method_candidates, denormalize_query, is_java_reserved,
};
pub(crate) use denormalize::{partial_proto, proto_matches};

pub use mappings::{FieldRef, MappingError, MappingsHash, MethodRef, NameMappings, NameResolver};
pub use proguard::{ProguardError, ProguardMapping, ProguardWarning, WarningKind};

/// Turn `com.poc.Main` / `com/poc/Main` / `Lcom/poc/Main;` into a Dalvik descriptor.
/// Array descriptors (`[Ljava/lang/Object;`, whose methods such as `clone`
/// are referenced like any class's) are kept as they are.
///
/// # Errors
///
/// [`EngineError::Query`] for an empty name.
pub fn format_class_name(name: &str) -> Result<String> {
    if name.is_empty() {
        return Err(EngineError::Query("Class name cannot be empty".into()));
    }
    if (name.starts_with('L') && name.ends_with(';') && name.contains('/')) || name.starts_with('[') {
        return Ok(name.to_string());
    }
    let mut n = name.replace('.', "/");
    if !n.starts_with('L') {
        n.insert(0, 'L');
    }
    if !n.ends_with(';') {
        n.push(';');
    }
    Ok(n)
}

/// `Lcom/poc/Main;` → `com/poc/Main`: the descriptor's own `L` / `;` removed,
/// package separators kept. Only the leading `L` is syntax, so
/// `Lpkg/LLoader;` → `pkg/LLoader` while `LLoader;` is class `Loader`.
pub(crate) fn class_inner(descriptor: &str) -> &str {
    let d = descriptor.strip_prefix('L').unwrap_or(descriptor);
    d.strip_suffix(';').unwrap_or(d)
}

/// Normalise a `listclass` prefix: `com.poc` / `com/poc` become `Lcom/poc`,
/// a prefix already starting with `L` is used as is (like the Python tool).
pub(crate) fn class_prefix(prefix: &str) -> Result<String> {
    let p = prefix.trim();
    if p.is_empty() {
        return Err(EngineError::Query("Class prefix cannot be empty".into()));
    }
    if p.starts_with('L') {
        return Ok(p.to_string());
    }
    Ok(format!("L{}", p.replace('.', "/")))
}

/// Normalise a class query: fuzzy patterns only get their dots swapped for
/// slashes, precise names become full descriptors.
pub(crate) fn normalize_class_query(name: &str, fuzzy: bool) -> Result<String> {
    if fuzzy {
        if name.contains('.') && !name.contains('/') {
            return Ok(name.replace('.', "/"));
        }
        return Ok(name.to_string());
    }
    format_class_name(name)
}

/// A method / field target as agents write it, split into its parts. Every
/// field borrows from the spec.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MemberTarget<'a> {
    /// The class part, as written (`Lcom/a/B;`, `com.a.B`, `B`, `~pattern`).
    pub class: Option<&'a str>,
    /// The member name (or name pattern).
    pub name: &'a str,
    /// What followed the name: `(params)ret` of a method, `:T` of a field.
    pub signature: Option<&'a str>,
}

impl<'a> MemberTarget<'a> {
    /// Split `Lcom/a/B;->name(sig)ret`, `Lcom/a/B;->name`, `Lc;->f:T`,
    /// `com.a.B->name`, `com.a.B#name`, `com.a.B.name` or `B.name`; a bare
    /// name (or name pattern) has no class. A dot only separates the class
    /// when what precedes it reads as a class (identifiers, the last one
    /// capitalised: `WebView.loadUrl`, not the regex `get.*Id`).
    pub(crate) fn parse(spec: &'a str) -> MemberTarget<'a> {
        let s = spec.trim();
        let (class, member) = if let Some((c, m)) = s.split_once("->") {
            (Some(c.trim()), m.trim())
        } else if let Some((c, m)) = s.rsplit_once('#') {
            (Some(c.trim()), m.trim())
        } else if let Some(i) = dotted_member_split(s) {
            (Some(&s[..i]), &s[i + 1..])
        } else {
            (None, s)
        };
        let (name, signature) = match member.find(['(', ':']) {
            Some(i) if is_member_name(&member[..i]) => (&member[..i], Some(&member[i..])),
            _ => (member, None),
        };
        MemberTarget { class: class.filter(|c| !c.is_empty()), name, signature }
    }
}

/// `[A-Za-z_$][A-Za-z0-9_$]*`, the identifiers of Java class paths and members
/// (obfuscators stay within it; non-ASCII names are left to `->`), or a
/// Kotlin backticked name (`` `in` ``).
fn is_identifier(s: &str) -> bool {
    if let Some(inner) = s.strip_prefix('`').and_then(|r| r.strip_suffix('`')) {
        return !inner.is_empty() && !inner.contains('`');
    }
    let mut chars = s.chars();
    chars.next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_' || c == '$')
        && chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
}

fn is_member_name(s: &str) -> bool {
    is_identifier(s) || s == "<init>" || s == "<clinit>"
}

/// The index of the dot separating `com.a.B` from `name` in `com.a.B.name`
/// (also `com/a/B.name`), if `spec` reads that way.
fn dotted_member_split(spec: &str) -> Option<usize> {
    let head_end = spec.find(['(', ':']).unwrap_or(spec.len());
    let head = &spec[..head_end];
    let i = head.rfind('.')?;
    let (class, name) = (&head[..i], &head[i + 1..]);
    let segments: Vec<&str> = class.split(['.', '/']).collect();
    let capitalised = segments.last().is_some_and(|l| l.trim_matches('`').starts_with(|c: char| c.is_ascii_uppercase()));
    let classlike = segments.iter().all(|s| is_identifier(s));
    // A signature or a constructor name settles it; otherwise Java naming does.
    let member_evident = head_end < spec.len() || name.starts_with('<');
    (classlike && is_member_name(name) && (capitalised || member_evident)).then_some(i)
}

/// Whether `spec` names a class rather than a member: `Lcom/a/B;` or a dotted
/// `com.a.B` whose last part is capitalised.
pub(crate) fn looks_like_class(spec: &str) -> bool {
    let s = spec.trim();
    if s.starts_with('L') && s.ends_with(';') && !s.contains("->") {
        return true;
    }
    let segments: Vec<&str> = s.split('.').collect();
    segments.len() > 1
        && segments.iter().all(|seg| is_identifier(seg))
        && segments.last().is_some_and(|l| l.trim_matches('`').starts_with(|c: char| c.is_ascii_uppercase()))
}

/// A class written without its package (`WebView`, `Build$VERSION`): a fuzzy
/// pattern for every class of that simple name. Fuzzy patterns run over the
/// whole string data, so the descriptor's end is its `;` and NUL terminator.
/// A descriptor (`Lx;`) is exact even without a package.
fn simple_class_pattern(name: &str) -> Option<String> {
    (is_identifier(name) && name != "L").then(|| format!("[/L]{};\\x00", regex::escape(name)))
}

/// Build a member (method/field) query from CLI-style arguments; `exact_name`
/// matches the member name as a whole instead of as a substring / regex.
///
/// Lenient about where the class is written: `name` may carry it
/// (`Lcom/a/B;->name`, `com.a.B->name`, `com.a.B#name`, `B.name`, ...; used
/// when `class` is not given), `class`
/// may carry `->name`, and a `name` that is only a class becomes the class.
/// A signature after the name (`run(I)V`, `f:I`) makes the name exact; the
/// rows show each overload's signature. In the class, `~` in front means
/// fuzzy; in the name, `=` in front means exact. A class without package
/// (`WebView`) matches that simple name in any package.
///
/// A signature after the name also narrows the query to it: `run(I)V` is
/// that overload, `run(I)` any return type, `f:I` the field of that type.
/// Types are Dalvik descriptors run together or comma-separated Java types
/// (`(int, java.lang.String) void`).
///
/// # Errors
///
/// [`EngineError::Query`] when neither a class nor a name is given, the
/// class is empty, or the signature is malformed.
pub fn member_query(kind: &str, class: Option<&str>, fuzzy_class: bool, name: Option<&str>, exact_name: bool) -> Result<MemberQuery> {
    let mut class = class.map(str::trim).filter(|c| !c.is_empty());
    let mut name = name.map(str::trim).filter(|n| !n.is_empty());
    let mut exact_name = exact_name;
    let mut signature = None;
    // `class: "Lc;->m"`: the whole target, read as one below.
    if let (Some(c), None) = (class, name)
        && (c.contains("->") || c.contains('#'))
    {
        (class, name) = (None, Some(c));
    }
    if let Some(n) = name {
        let t = MemberTarget::parse(n);
        match t.class {
            Some(c) => {
                class = class.or(Some(c));
                name = Some(t.name).filter(|n| !n.is_empty());
            }
            None if class.is_none() && looks_like_class(n) => (class, name) = (Some(n), None),
            None => name = Some(t.name),
        }
        if let Some(sig) = t.signature {
            exact_name = true;
            signature = Some(parse_signature(sig, kind == "method")?);
        }
    }
    if let Some(n) = name.and_then(|n| n.strip_prefix('=')) {
        (name, exact_name) = (Some(n), true);
    }
    if class.is_none() && name.is_none() {
        return Err(EngineError::Query(format!(
            "{kind} query needs a class or a {kind} name, e.g. Landroid/webkit/WebView;->loadUrl, android.webkit.WebView.loadUrl or just loadUrl"
        )));
    }
    let class = match class {
        Some(c) => Some(match c.strip_prefix('~') {
            Some(f) => ClassQuery::fuzzy(normalize_class_query(f, true)?),
            None if fuzzy_class => ClassQuery::fuzzy(normalize_class_query(c, true)?),
            None => match simple_class_pattern(c) {
                Some(pattern) => ClassQuery::fuzzy(pattern),
                None => ClassQuery::exact(format_class_name(c)?),
            },
        }),
        None => None,
    };
    Ok(MemberQuery::default().with_class(class).with_name(name.map(String::from)).with_exact_name(exact_name).with_signature(signature))
}

/// The findrefs kinds, as conditions spell them (`literal` is a string
/// matched as plain text).
pub const KINDS: &[&str] = &["string", "literal", "type", "method", "field", "extends", "impl", "override", "annotation", "flag", "in"];

/// The canonical kind for what an agent wrote: case and plurals do not
/// matter (`Methods`), nor common synonyms (`implements`, `class`, `call`).
/// Unknown kinds fail with the valid ones and the closest of them.
///
/// # Errors
///
/// [`EngineError::Query`] for an unknown kind.
pub fn parse_kind(kind: &str) -> Result<&'static str> {
    let k = kind.trim().to_ascii_lowercase();
    let alias = |k: &str| -> Option<&'static str> {
        Some(match k {
            "string" | "str" | "text" => "string",
            "literal" => "literal",
            "type" | "class" => "type",
            "method" | "call" | "invoke" => "method",
            "field" => "field",
            "extends" | "extend" | "subclass" | "superclass" => "extends",
            "impl" | "implements" | "implement" | "interface" | "implementer" => "impl",
            "override" | "overrides" | "implementor" => "override",
            "annotation" | "annotated" | "anno" => "annotation",
            "flag" | "flags" | "access" | "modifier" => "flag",
            "in" | "inclass" => "in",
            _ => return None,
        })
    };
    alias(&k).or_else(|| k.strip_suffix("es").and_then(alias)).or_else(|| k.strip_suffix('s').and_then(alias)).ok_or_else(|| {
        let closest = KINDS.iter().min_by_key(|c| edit_distance(&k, c, usize::MAX).unwrap_or(usize::MAX)).copied().unwrap_or("string");
        EngineError::Query(format!("unknown kind {kind:?}: use string, type, method, field, extends or impl (closest: {closest})"))
    })
}

/// The Levenshtein distance of `a` and `b`, or `None` once it exceeds `max`.
pub(crate) fn edit_distance(a: &str, b: &str, max: usize) -> Option<usize> {
    let (a, b): (Vec<char>, Vec<char>) = (a.chars().collect(), b.chars().collect());
    if a.len().abs_diff(b.len()) > max {
        return None;
    }
    let mut row: Vec<usize> = (0..=b.len()).collect();
    for (i, ca) in a.iter().enumerate() {
        let mut prev = row[0];
        row[0] = i + 1;
        let mut best = row[0];
        for (j, cb) in b.iter().enumerate() {
            let cur = row[j + 1];
            row[j + 1] = if ca == cb { prev } else { 1 + prev.min(cur).min(row[j]) };
            prev = cur;
            best = best.min(row[j + 1]);
        }
        if best > max {
            return None;
        }
    }
    Some(row[b.len()]).filter(|&d| d <= max)
}

/// `loadUrl(java.lang.String, int)` → `loadUrl(Ljava/lang/String;I)`: a
/// signature with Java parameter types in Dalvik form (a return type, if
/// any, likewise). `None` when it has no parameter list or a type is not
/// recognisable.
pub fn java_signature_to_dalvik(signature: &str) -> Option<String> {
    let (name, rest) = signature.trim().split_once('(')?;
    let (params, ret) = rest.split_once(')')?;
    let params: String = params.split(',').map(str::trim).filter(|p| !p.is_empty()).map(java_type).collect::<Option<_>>()?;
    let ret = match ret.trim() {
        "" => String::new(),
        r => java_type(r)?,
    };
    Some(format!("{}({params}){ret}", name.trim()))
}

/// `int` → `I`, `java.lang.String[]` → `[Ljava/lang/String;`, descriptors as they are.
fn java_type(t: &str) -> Option<String> {
    // A loop, not recursion: the dimensions come from the user's query.
    let mut t = t.trim();
    let mut dims = 0;
    while let Some(inner) = t.strip_suffix("[]") {
        t = inner.trim();
        dims += 1;
    }
    let element = java_element_type(t)?;
    Some("[".repeat(dims) + &element)
}

/// [`java_type`] of a type that is not a Java array.
fn java_element_type(t: &str) -> Option<String> {
    let primitive = match t {
        "void" => "V",
        "boolean" => "Z",
        "byte" => "B",
        "short" => "S",
        "char" => "C",
        "int" => "I",
        "long" => "J",
        "float" => "F",
        "double" => "D",
        _ => "",
    };
    if !primitive.is_empty() {
        return Some(primitive.to_string());
    }
    if (t.starts_with('L') && t.ends_with(';')) || t.starts_with('[') || (t.len() == 1 && "VZBSCIJFD".contains(t)) {
        return Some(t.to_string());
    }
    t.split('.').all(is_identifier).then(|| format!("L{};", t.replace('.', "/")))
}

/// The signature written after a member name: `(params)ret` or `(params)`
/// of a method, `:T` of a field. Types are Dalvik descriptors run together
/// (`(ILjava/lang/String;)V`) or Java types separated by commas
/// (`(int, java.lang.String) void`); a Java class without package
/// (`String`) is that simple name in any package.
pub(crate) fn parse_signature(signature: &str, method: bool) -> Result<MemberSignature> {
    let bad = |why: &str| {
        EngineError::Query(format!(
            "bad signature {signature:?}: {why}; write a method's as (params)ret, e.g. (ILjava/lang/String;)V or (int, String), \
             a field's as :T, e.g. :I"
        ))
    };
    let s = signature.trim();
    if !method {
        let t = s.strip_prefix(':').ok_or_else(|| bad("a field takes :T"))?;
        return type_match(t).map(MemberSignature::Field).ok_or_else(|| bad("unknown type"));
    }
    let (params, ret) = s.strip_prefix('(').and_then(|r| r.split_once(')')).ok_or_else(|| bad("a method takes (params)"))?;
    let params = params.trim();
    let params = if params.is_empty() {
        Vec::new()
    } else if let Some(descriptors) = (!params.contains([',', ' '])).then(|| dalvik_types(params)).flatten() {
        descriptors.into_iter().map(|d| TypeMatch::Descriptor(d.to_string())).collect()
    } else {
        params.split(',').map(type_match).collect::<Option<_>>().ok_or_else(|| bad("unknown parameter type"))?
    };
    let ret = match ret.trim() {
        "" => None,
        r => Some(type_match(r).ok_or_else(|| bad("unknown return type"))?),
    };
    Ok(MemberSignature::Method { params, ret })
}

/// `ILjava/lang/String;[B` → `["I", "Ljava/lang/String;", "[B"]`; `None`
/// unless all of `s` is descriptors.
pub(crate) fn dalvik_types(s: &str) -> Option<Vec<&str>> {
    let mut out = Vec::new();
    let mut rest = s;
    while !rest.is_empty() {
        let dims = rest.len() - rest.trim_start_matches('[').len();
        let end = match rest[dims..].chars().next()? {
            'Z' | 'B' | 'S' | 'C' | 'I' | 'J' | 'F' | 'D' | 'V' => dims + 1,
            'L' => dims + rest[dims..].find(';')? + 1,
            _ => return None,
        };
        out.push(&rest[..end]);
        rest = &rest[end..];
    }
    Some(out)
}

/// One type as written in a signature: a descriptor, a Java primitive, a
/// dotted or slashed class (`java.lang.String`), a class without package
/// (`String`, any package), each optionally followed by `[]`s.
fn type_match(t: &str) -> Option<TypeMatch> {
    let t = t.trim();
    if let Some([d]) = dalvik_types(t).as_deref() {
        return Some(TypeMatch::Descriptor(d.to_string()));
    }
    let (mut base, mut dims) = (t, 0);
    while let Some(b) = base.strip_suffix("[]") {
        (base, dims) = (b.trim_end(), dims + 1);
    }
    let arrays = "[".repeat(dims);
    let primitive = match base {
        "void" => "V",
        "boolean" => "Z",
        "byte" => "B",
        "short" => "S",
        "char" => "C",
        "int" => "I",
        "long" => "J",
        "float" => "F",
        "double" => "D",
        _ => "",
    };
    if !primitive.is_empty() {
        return Some(TypeMatch::Descriptor(format!("{arrays}{primitive}")));
    }
    if base.contains(['.', '/']) {
        return base.split(['.', '/']).all(is_identifier).then(|| TypeMatch::Descriptor(format!("{arrays}L{};", base.replace('.', "/"))));
    }
    is_identifier(base).then(|| TypeMatch::Simple { name: base.to_string(), dims })
}

/// The query of exactly the method `signature` names, `Lcls;->name(params)ret`
/// as findrefs and callgraph print it (whatever characters the name holds).
pub(crate) fn exact_method(signature: &str) -> Result<MemberQuery> {
    let bad = || EngineError::Query(format!("not a method signature: {signature:?} (want Lcls;->name(params)ret)"));
    let (class, member) = signature.trim().split_once("->").ok_or_else(bad)?;
    let p = member.find('(').ok_or_else(bad)?;
    Ok(MemberQuery::default()
        .with_class(Some(ClassQuery::exact(class.to_string())))
        .with_name(Some(member[..p].to_string()))
        .with_exact_name(true)
        .with_signature(Some(parse_signature(&member[p..], true)?)))
}

/// Parse a one-word findrefs condition, as the CLI's compound and rule
/// options take them:
///
/// - `string:REGEX`, `literal:TEXT`, `type:PATTERN`
/// - `method:TARGET`, `field:TARGET`: any member target form (as in
///   [`member_query`]), e.g.
///   `android.webkit.WebView->loadUrl`; the class is exact unless prefixed
///   `~` (fuzzy) or written without package, the name a regex / substring
///   unless prefixed `=` or followed by a signature (exact)
/// - `method:{RULE}`: calls of the methods whose code matches RULE
/// - `method:@FILE`: calls of the methods FILE lists, one per line
/// - `extends:CLASS`, `impl:CLASS`
///
/// The kind is read by [`parse_kind`] (`methods:`, `Method:` work too); the
/// grammar is [`crate::condition`]'s.
///
/// # Errors
///
/// [`EngineError::Query`] for a syntax error, an unknown kind, an invalid
/// value, or a rule of several conditions.
pub fn parse_condition(spec: &str) -> Result<Query> {
    crate::condition::parse_condition_in(spec, None)
}

/// One rule of a rule set: a [`parse_condition`] condition, or several
/// joined with `&` (a compound: every part must match in one method, none
/// of the `!` parts).
///
/// # Errors
///
/// [`EngineError::Query`] for a syntax error, an unknown kind or an invalid
/// value.
pub fn parse_rule(spec: &str) -> Result<Query> {
    crate::condition::parse_rule_in(spec, None)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn class_names() {
        assert_eq!(format_class_name("com.poc.Main").unwrap(), "Lcom/poc/Main;");
        assert_eq!(format_class_name("Lcom/poc/Main;").unwrap(), "Lcom/poc/Main;");
        assert_eq!(format_class_name("com/poc/Main").unwrap(), "Lcom/poc/Main;");
        assert_eq!(format_class_name("[Ljava/lang/Object;").unwrap(), "[Ljava/lang/Object;");
        assert_eq!(normalize_class_query("com.poc", true).unwrap(), "com/poc");
        assert_eq!(normalize_class_query("poc", true).unwrap(), "poc");
        assert!(format_class_name("").is_err());
        for p in ["com.xxx", "com/xxx", "Lcom/xxx", " com.xxx\t"] {
            assert_eq!(class_prefix(p).unwrap(), "Lcom/xxx");
        }
        assert_eq!(class_prefix("a").unwrap(), "La");
        assert_eq!(class_prefix(" ").unwrap_err().to_string(), "Class prefix cannot be empty");
        assert!(member_query("method", None, false, None, false).is_err());
        let q = member_query("method", Some("com.poc.Main"), false, Some("on"), false).unwrap();
        assert!(!q.exact_name);
        assert_eq!(q.class.unwrap(), ClassQuery::exact("Lcom/poc/Main;"));
        assert!(member_query("field", None, false, Some("f"), true).unwrap().exact_name);
    }

    fn precise(c: &str) -> Option<ClassQuery> {
        Some(ClassQuery::exact(c))
    }

    #[test]
    fn member_targets_in_every_spelling() {
        let t = |s| MemberTarget::parse(s);
        assert_eq!(t("Lcom/a/B;->run(I)V"), MemberTarget { class: Some("Lcom/a/B;"), name: "run", signature: Some("(I)V") });
        assert_eq!(t("Lc;->f:I"), MemberTarget { class: Some("Lc;"), name: "f", signature: Some(":I") });
        assert_eq!(t("com.a.B->run"), MemberTarget { class: Some("com.a.B"), name: "run", signature: None });
        assert_eq!(t("com.a.B#run"), MemberTarget { class: Some("com.a.B"), name: "run", signature: None });
        assert_eq!(
            t("android.webkit.WebView.loadUrl"),
            MemberTarget { class: Some("android.webkit.WebView"), name: "loadUrl", signature: None }
        );
        assert_eq!(t("com/a/B.run(I)V"), MemberTarget { class: Some("com/a/B"), name: "run", signature: Some("(I)V") });
        assert_eq!(t("java.lang.Thread.<init>"), MemberTarget { class: Some("java.lang.Thread"), name: "<init>", signature: None });
        assert_eq!(t("Build$VERSION.SDK_INT").class, Some("Build$VERSION"));
        // Patterns and lowercase dotted names stay names.
        for name in ["get.*Id", "(get|set)Foo", "a.b", "exec"] {
            assert_eq!(t(name), MemberTarget { class: None, name, signature: None }, "{name}");
        }
        assert!(looks_like_class("android.webkit.WebView") && looks_like_class("Lcom/a/B;"));
        assert!(!looks_like_class("loadUrl") && !looks_like_class("com.a.b") && !looks_like_class("Lc;->m"));
    }

    #[test]
    fn member_queries_from_any_spelling_agree() {
        let want = MemberQuery::default().with_class(precise("Landroid/webkit/WebView;")).with_name(Some("loadUrl".into()));
        for spec in [
            "Landroid/webkit/WebView;->loadUrl",
            "android.webkit.WebView.loadUrl",
            "android.webkit.WebView->loadUrl",
            "android.webkit.WebView#loadUrl",
        ] {
            assert_eq!(member_query("method", None, false, Some(spec), false).unwrap(), want, "{spec}");
        }
        // The class may also carry the member, or come on its own.
        assert_eq!(member_query("method", Some("Landroid/webkit/WebView;->loadUrl"), false, None, false).unwrap(), want);
        assert_eq!(member_query("method", Some("android.webkit.WebView"), false, Some("loadUrl"), false).unwrap(), want);
        let class_only = member_query("method", None, false, Some("android.webkit.WebView"), false).unwrap();
        assert_eq!(class_only, MemberQuery::default().with_class(precise("Landroid/webkit/WebView;")));
        // A signature or `=` makes the name exact; an explicit class wins.
        let q = member_query("method", None, false, Some("Lc;->run(I)V"), false).unwrap();
        assert_eq!((q.name.as_deref(), q.exact_name), (Some("run"), true));
        let q = member_query("field", Some("Lx;"), false, Some("Lc;->f:I"), false).unwrap();
        assert_eq!((q.class, q.name.as_deref(), q.exact_name), (precise("Lx;"), Some("f"), true));
        let q = member_query("method", None, false, Some("~Foo->=bar"), false).unwrap();
        assert_eq!(
            q,
            MemberQuery::default().with_class(Some(ClassQuery::fuzzy("Foo"))).with_name(Some("bar".into())).with_exact_name(true)
        );
        // Without a package: that simple name in any package.
        let q = member_query("method", None, false, Some("WebView.loadUrl"), false).unwrap();
        assert_eq!(q.class, Some(ClassQuery::fuzzy("[/L]WebView;\\x00")));
        let e = member_query("method", None, false, Some(" "), false).unwrap_err().to_string();
        assert!(e.contains("needs a class or a method name"), "{e}");
    }

    #[test]
    fn kinds_tolerate_case_plurals_and_synonyms() {
        for (k, want) in [
            ("Methods", "method"),
            ("strings", "string"),
            ("TYPE", "type"),
            ("implements", "impl"),
            ("classes", "type"),
            ("fields", "field"),
            ("literal", "literal"),
        ] {
            assert_eq!(parse_kind(k).unwrap(), want, "{k}");
        }
        let e = parse_kind("metod").unwrap_err().to_string();
        assert!(e.contains("unknown kind \"metod\"") && e.contains("closest: method"), "{e}");
        assert_eq!(
            parse_condition("Methods:android.webkit.WebView.loadUrl").unwrap(),
            parse_condition("method:Landroid/webkit/WebView;->loadUrl").unwrap()
        );
        assert!(matches!(parse_rule("string:a & method:b").unwrap(), Query::All { all, .. } if all.len() == 2));
    }

    #[test]
    fn signatures_in_dalvik_and_java_form() {
        let d = |s: &str| TypeMatch::Descriptor(s.into());
        let method = |params: Vec<TypeMatch>, ret: Option<TypeMatch>| MemberSignature::Method { params, ret };
        let sig = |s| parse_signature(s, true).unwrap();
        assert_eq!(sig("(I)V"), method(vec![d("I")], Some(d("V"))));
        assert_eq!(sig("(I)"), method(vec![d("I")], None));
        assert_eq!(sig("()"), method(vec![], None));
        assert_eq!(sig("(ILjava/lang/String;[B)Z"), method(vec![d("I"), d("Ljava/lang/String;"), d("[B")], Some(d("Z"))));
        assert_eq!(sig("(int)"), method(vec![d("I")], None));
        assert_eq!(sig("(int, java.lang.String[]) void"), method(vec![d("I"), d("[Ljava/lang/String;")], Some(d("V"))));
        assert_eq!(
            sig("(String, Map[][])"),
            method(vec![TypeMatch::Simple { name: "String".into(), dims: 0 }, TypeMatch::Simple { name: "Map".into(), dims: 2 }], None)
        );
        assert_eq!(parse_signature(":I", false).unwrap(), MemberSignature::Field(d("I")));
        assert_eq!(parse_signature(":java.lang.String", false).unwrap(), MemberSignature::Field(d("Ljava/lang/String;")));
        for (bad, method) in [("(I", true), ("(%)V", true), (":I", true), ("(I)V", false), (":", false)] {
            let e = parse_signature(bad, method).unwrap_err().to_string();
            assert!(e.contains("bad signature"), "{bad}: {e}");
        }
        // A signature after the name narrows the query to it.
        for spec in ["Lc;->run(I)V", "c.run(int)void", "Lc;->run(I)V "] {
            let q = member_query("method", None, false, Some(spec), false).unwrap();
            assert_eq!(q.signature, Some(method(vec![d("I")], Some(d("V")))), "{spec}");
        }
        assert_eq!(
            member_query("method", None, false, Some("WebView.loadUrl(String)"), false).unwrap().signature.unwrap().to_string(),
            "(String)"
        );
        assert_eq!(member_query("method", None, false, Some("loadUrl"), false).unwrap().signature, None);
        assert!(member_query("field", None, false, Some("Lc;->f(I)V"), false).is_err());
        assert_eq!(
            parse_condition("method:Lc;->m(I)").unwrap(),
            Query::Method(member_query("method", None, false, Some("Lc;->m(I)"), false).unwrap())
        );
    }

    #[test]
    fn java_signatures_and_edit_distance() {
        assert_eq!(java_signature_to_dalvik("loadUrl(java.lang.String, int)").as_deref(), Some("loadUrl(Ljava/lang/String;I)"));
        assert_eq!(java_signature_to_dalvik("f(byte[], Lx;) void").as_deref(), Some("f([BLx;)V"));
        assert_eq!(java_signature_to_dalvik("run()").as_deref(), Some("run()"));
        assert_eq!(java_signature_to_dalvik("run"), None);
        assert_eq!(java_signature_to_dalvik("f(int [] [])").as_deref(), Some("f([[I)"));
        // Array dimensions are counted, not recursed into.
        let deep = format!("f(int{})", "[]".repeat(1_000_000));
        assert_eq!(java_signature_to_dalvik(&deep).map(|s| s.len()), Some("f()I".len() + 1_000_000));
        assert_eq!(edit_distance("kitten", "sitting", 5), Some(3));
        assert_eq!(edit_distance("kitten", "sitting", 2), None);
        assert_eq!(edit_distance("a", "a", 0), Some(0));
    }
}