165 lines
5 KiB
Rust
165 lines
5 KiB
Rust
//! Byte-level builders for replay scripts, written independently of the crate's encoder.
|
|
|
|
#![allow(dead_code)]
|
|
|
|
use tunal_devices::mtp::Step;
|
|
|
|
pub const PACKET: usize = 512;
|
|
pub const STORAGE: u32 = 0x0001_0001;
|
|
|
|
pub fn pattern(len: usize, seed: u32) -> Vec<u8> {
|
|
let mut x = seed;
|
|
(0..len)
|
|
.map(|_| {
|
|
x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345);
|
|
(x >> 16) as u8
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub fn container(kind: u16, code: u16, tid: u32, payload: &[u8]) -> Vec<u8> {
|
|
let mut out = Vec::new();
|
|
out.extend_from_slice(&((12 + payload.len()) as u32).to_le_bytes());
|
|
out.extend_from_slice(&kind.to_le_bytes());
|
|
out.extend_from_slice(&code.to_le_bytes());
|
|
out.extend_from_slice(&tid.to_le_bytes());
|
|
out.extend_from_slice(payload);
|
|
out
|
|
}
|
|
|
|
pub fn params_bytes(params: &[u32]) -> Vec<u8> {
|
|
params.iter().flat_map(|p| p.to_le_bytes()).collect()
|
|
}
|
|
|
|
pub fn u32s(values: &[u32]) -> Vec<u8> {
|
|
let mut out = (values.len() as u32).to_le_bytes().to_vec();
|
|
out.extend(params_bytes(values));
|
|
out
|
|
}
|
|
|
|
pub fn u16s(values: &[u16]) -> Vec<u8> {
|
|
let mut out = (values.len() as u32).to_le_bytes().to_vec();
|
|
out.extend(values.iter().flat_map(|v| v.to_le_bytes()));
|
|
out
|
|
}
|
|
|
|
pub fn ptp_string(s: &str) -> Vec<u8> {
|
|
if s.is_empty() {
|
|
return vec![0];
|
|
}
|
|
let units: Vec<u16> = s.encode_utf16().chain([0]).collect();
|
|
let mut out = vec![units.len() as u8];
|
|
out.extend(units.iter().flat_map(|u| u.to_le_bytes()));
|
|
out
|
|
}
|
|
|
|
pub fn device_info() -> Vec<u8> {
|
|
let ops = [
|
|
0x1001, 0x1002, 0x1003, 0x1004, 0x1005, 0x1007, 0x1008, 0x1009, 0x100B, 0x100C, 0x100D,
|
|
0x1019, 0x101A, 0x101B, 0x9803, 0x9804, 0x9805, 0x95C1,
|
|
];
|
|
let mut d = Vec::new();
|
|
d.extend(100u16.to_le_bytes());
|
|
d.extend(6u32.to_le_bytes());
|
|
d.extend(100u16.to_le_bytes());
|
|
d.extend(ptp_string("microsoft.com: 1.0; android.com: 1.0;"));
|
|
d.extend(0u16.to_le_bytes());
|
|
d.extend(u16s(&ops));
|
|
d.extend(u16s(&[0x4002, 0x4003]));
|
|
d.extend(u16s(&[0xD401]));
|
|
d.extend(u16s(&[]));
|
|
d.extend(u16s(&[0x3000, 0x3001, 0x3009]));
|
|
for s in ["Example Phones", "Model X", "1.0", "SER123"] {
|
|
d.extend(ptp_string(s));
|
|
}
|
|
d
|
|
}
|
|
|
|
pub fn storage_info() -> Vec<u8> {
|
|
let mut d = Vec::new();
|
|
d.extend(3u16.to_le_bytes()); // fixed RAM
|
|
d.extend(2u16.to_le_bytes()); // hierarchical
|
|
d.extend(0u16.to_le_bytes());
|
|
d.extend(64_000_000_000u64.to_le_bytes());
|
|
d.extend(12_345_678_901u64.to_le_bytes());
|
|
d.extend(0xFFFF_FFFFu32.to_le_bytes());
|
|
d.extend(ptp_string("Internal shared storage"));
|
|
d.extend(ptp_string(""));
|
|
d
|
|
}
|
|
|
|
pub fn object_info(format: u16, size: u32, parent: u32, name: &str, modified: &str) -> Vec<u8> {
|
|
let mut d = Vec::new();
|
|
d.extend(STORAGE.to_le_bytes());
|
|
d.extend(format.to_le_bytes());
|
|
d.extend(0u16.to_le_bytes());
|
|
d.extend(size.to_le_bytes());
|
|
d.extend(0u16.to_le_bytes());
|
|
d.extend([0u8; 4 * 6]);
|
|
d.extend(parent.to_le_bytes());
|
|
d.extend((if format == 0x3001 { 1u16 } else { 0 }).to_le_bytes());
|
|
d.extend([0u8; 8]);
|
|
d.extend(ptp_string(name));
|
|
d.extend(ptp_string(""));
|
|
d.extend(ptp_string(modified));
|
|
d.extend(ptp_string(""));
|
|
d
|
|
}
|
|
|
|
/// SendObjectInfo dataset as the client sends it (no dates).
|
|
pub fn new_object_info(name: &str, size: u32, parent: u32, folder: bool) -> Vec<u8> {
|
|
let mut d = Vec::new();
|
|
d.extend(STORAGE.to_le_bytes());
|
|
d.extend((if folder { 0x3001u16 } else { 0x3000 }).to_le_bytes());
|
|
d.extend(0u16.to_le_bytes());
|
|
d.extend(size.to_le_bytes());
|
|
d.extend(0u16.to_le_bytes());
|
|
d.extend([0u8; 4 * 6]);
|
|
d.extend(parent.to_le_bytes());
|
|
d.extend((if folder { 1u16 } else { 0 }).to_le_bytes());
|
|
d.extend([0u8; 8]);
|
|
d.extend(ptp_string(name));
|
|
d.extend(ptp_string(""));
|
|
d.extend(ptp_string(""));
|
|
d.extend(ptp_string(""));
|
|
d
|
|
}
|
|
|
|
/// A replay script built one transaction at a time; transaction ids count from 0.
|
|
#[derive(Default)]
|
|
pub struct Script {
|
|
pub steps: Vec<Step>,
|
|
tid: u32,
|
|
}
|
|
|
|
impl Script {
|
|
/// Command `op`, then the host's data `out` or the device's `data` (with or without a
|
|
/// zero-length packet), then response `code` with `response` parameters.
|
|
pub fn exchange(
|
|
&mut self,
|
|
op: u16,
|
|
params: &[u32],
|
|
out: Option<Vec<u8>>,
|
|
data: Option<(Vec<u8>, bool)>,
|
|
code: u16,
|
|
response: &[u32],
|
|
) {
|
|
let tid = self.tid;
|
|
self.steps
|
|
.push(Step::Out(container(1, op, tid, ¶ms_bytes(params))));
|
|
if let Some(out) = out {
|
|
self.steps.push(Step::Out(container(2, op, tid, &out)));
|
|
}
|
|
if let Some((data, zlp)) = data {
|
|
self.steps.push(Step::In {
|
|
data: container(2, op, tid, &data),
|
|
zlp,
|
|
});
|
|
}
|
|
self.steps.push(Step::In {
|
|
data: container(3, code, tid, ¶ms_bytes(response)),
|
|
zlp: true,
|
|
});
|
|
self.tid += 1;
|
|
}
|
|
}
|