Skip to main content

iphone_cli/ax/
request.rs

1//! One question to the phone's accessibility translator: "what is attribute N
2//! of this element?". Sent with `processDataFromHost:`, answered within tens of
3//! milliseconds, without the 0.75 s the phone waits before a whole-screen dump.
4//!
5//! The archive is the one the Mac's own `AXPHostCacheManager` writes for an
6//! `AXPTranslatorRequest` (macOS 27, captured 2026-11-12), wrapped in an
7//! archived NSData as every DTX object argument is.
8
9use plist::{Dictionary, Uid, Value};
10
11use super::dump::ObjKey;
12
13/// `AXPTranslatorRequest.requestType` for one attribute of one element. (The
14/// phone files the answer under its own numbering, which differs.)
15const ATTRIBUTE_REQUEST: u64 = 2;
16
17fn class(name: &str, chain: &[&str]) -> Value {
18    let mut d = Dictionary::new();
19    d.insert("$classname".into(), Value::String(name.into()));
20    d.insert(
21        "$classes".into(),
22        Value::Array(chain.iter().map(|c| Value::String((*c).into())).collect()),
23    );
24    Value::Dictionary(d)
25}
26
27fn archive(objects: Vec<Value>) -> Vec<u8> {
28    let mut top = Dictionary::new();
29    top.insert("root".into(), Value::Uid(Uid::new(1)));
30    let mut d = Dictionary::new();
31    d.insert("$version".into(), Value::Integer(100_000.into()));
32    d.insert("$archiver".into(), Value::String("NSKeyedArchiver".into()));
33    d.insert("$top".into(), Value::Dictionary(top));
34    d.insert("$objects".into(), Value::Array(objects));
35    let mut out = Vec::new();
36    Value::Dictionary(d)
37        .to_writer_binary(&mut out)
38        .expect("writing a plist to memory cannot fail");
39    out
40}
41
42/// The bytes to send as the one argument of `processDataFromHost:`.
43pub fn attribute(key: ObjKey, attribute: i64) -> Vec<u8> {
44    let null = Value::String("$null".into());
45    let mut req = Dictionary::new();
46    req.insert("requestType".into(), Value::Integer(ATTRIBUTE_REQUEST.into()));
47    req.insert("attributeType".into(), Value::Integer(attribute.into()));
48    req.insert("actionType".into(), Value::Integer(0.into()));
49    req.insert("clientType".into(), Value::Integer(0.into()));
50    req.insert("translation".into(), Value::Uid(Uid::new(2)));
51    req.insert("parameters".into(), Value::Uid(Uid::new(0)));
52    req.insert("$class".into(), Value::Uid(Uid::new(5)));
53    let mut obj = Dictionary::new();
54    obj.insert("pid".into(), Value::Integer(key.pid.into()));
55    obj.insert("objectID".into(), Value::Uid(Uid::new(3)));
56    obj.insert("isApplicationElement".into(), Value::Boolean(false));
57    obj.insert("didPopuldateAppInfo".into(), Value::Boolean(true));
58    obj.insert("rawElementData".into(), Value::Uid(Uid::new(0)));
59    obj.insert("bridgeDelegateToken".into(), Value::Uid(Uid::new(0)));
60    obj.insert("$class".into(), Value::Uid(Uid::new(4)));
61    let inner = archive(vec![
62        null.clone(),
63        Value::Dictionary(req),
64        Value::Dictionary(obj),
65        Value::Integer(key.object_id.into()),
66        class("AXPTranslationObject", &["AXPTranslationObject", "NSObject"]),
67        class("AXPTranslatorRequest", &["AXPTranslatorRequest", "NSObject"]),
68    ]);
69    let mut data = Dictionary::new();
70    data.insert("NS.data".into(), Value::Data(inner));
71    data.insert("$class".into(), Value::Uid(Uid::new(2)));
72    archive(vec![
73        null,
74        Value::Dictionary(data),
75        class("NSMutableData", &["NSMutableData", "NSData", "NSObject"]),
76    ])
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use crate::ax::archive::{self, Ka};
83
84    #[test]
85    fn decodes_back_to_the_request() {
86        let key = ObjKey { pid: 2383, object_id: 13_597_625_945_791_341_321 };
87        let outer = archive::decode(&attribute(key, 21)).unwrap();
88        let Ka::Data(inner) = outer else { panic!("outer layer is not data: {outer:?}") };
89        let req = archive::decode(&inner).unwrap();
90        assert_eq!(req.class(), Some("AXPTranslatorRequest"));
91        assert_eq!(req.get("requestType").and_then(|v| v.as_u64()), Some(2));
92        assert_eq!(req.get("attributeType").and_then(|v| v.as_i64()), Some(21));
93        let t = req.get("translation").unwrap();
94        assert_eq!(ObjKey::from_ka(t), Some(key));
95    }
96}