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
// jkcoxson
use std::ffi::CString;
use crate::{bindings as unsafe_bindings, error::RestoredError, idevice::Device};
use plist_plus::Plist;
/// Restores an iDevice to a specific backup or iOS version
pub struct RestoredClient<'a> {
pub(crate) pointer: unsafe_bindings::restored_client_t,
phantom: std::marker::PhantomData<&'a Device>,
}
impl RestoredClient<'_> {
/// Starts a new connection and adds a restored client to it
/// # Arguments
/// * `device` - The device to connect to
/// * `label` - The label for the connection
/// # Returns
/// A struct containing the handle to the connection
///
/// ***Verified:*** False
pub fn new(device: &Device, label: impl Into<String>) -> Result<Self, RestoredError> {
let mut pointer = unsafe { std::mem::zeroed() };
let label_c_string = CString::new(label.into()).unwrap();
let result = unsafe {
unsafe_bindings::restored_client_new(
device.pointer,
&mut pointer,
label_c_string.as_ptr(),
)
}
.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(Self {
pointer,
phantom: std::marker::PhantomData,
})
}
/// Get the type of restored client
/// # Arguments
/// *none*
/// # Returns
/// A type and version of the client
///
/// ***Verified:*** False
pub fn query_type(&self) -> Result<(String, u64), RestoredError> {
let mut type_ = std::ptr::null_mut();
let mut version = 0;
let result =
unsafe { unsafe_bindings::restored_query_type(self.pointer, &mut type_, &mut version) }
.into();
if result != RestoredError::Success {
return Err(result);
}
let type_ = unsafe {
std::ffi::CStr::from_ptr(type_)
.to_string_lossy()
.into_owned()
};
Ok((type_, version))
}
/// Queries a value from the client
/// # Arguments
/// * `key` - The key to get the value for
/// # Returns
/// A plist with the returned value
///
/// ***Verified:*** False
pub fn query_value(&self, key: impl Into<String>) -> Result<Plist, RestoredError> {
let mut value = std::ptr::null_mut();
let key_c_string = CString::new(key.into()).unwrap();
let result = unsafe {
unsafe_bindings::restored_query_value(self.pointer, key_c_string.as_ptr(), &mut value)
}
.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(value.into())
}
/// Gets a value from the client
/// # Arguments
/// * `key` - The key to get the value for
/// # Returns
/// A plist with the returned value
///
/// ***Verified:*** False
pub fn get_value(&self, key: impl Into<String>) -> Result<Plist, RestoredError> {
let mut value = std::ptr::null_mut();
let key_c_string = CString::new(key.into()).unwrap();
let result = unsafe {
unsafe_bindings::restored_get_value(self.pointer, key_c_string.as_ptr(), &mut value)
}
.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(value.into())
}
/// Sends a message to the client
/// # Arguments
/// * `data` - The data to send as a plist
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn send(&self, data: Plist) -> Result<(), RestoredError> {
let result =
unsafe { unsafe_bindings::restored_send(self.pointer, data.get_pointer()) }.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(())
}
/// Receives a message from client
/// # Arguments
/// *none*
/// # Returns
/// A plist containing the response
///
/// ***Verified:*** False
pub fn receive(&self) -> Result<Plist, RestoredError> {
let mut value = std::ptr::null_mut();
let result = unsafe { unsafe_bindings::restored_receive(self.pointer, &mut value) }.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(value.into())
}
/// Sends a goodbye, terminating the connection
/// # Arguments
/// *none*
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn goodbye(self) -> Result<(), RestoredError> {
let result = unsafe { unsafe_bindings::restored_goodbye(self.pointer) }.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(())
}
/// Starts a restore of the device
/// # Arguments
/// * `options` - The options for the restore
/// * `version` - The restore protocol version
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn start_restore(&self, options: Option<Plist>, version: u64) -> Result<(), RestoredError> {
let ptr = if let Some(options) = options {
options.get_pointer()
} else {
std::ptr::null_mut()
};
let result =
unsafe { unsafe_bindings::restored_start_restore(self.pointer, ptr, version) }.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(())
}
/// Reboots the device
/// # Arguments
/// *none*
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn reboot(self) -> Result<(), RestoredError> {
let result = unsafe { unsafe_bindings::restored_reboot(self.pointer) }.into();
if result != RestoredError::Success {
return Err(result);
}
Ok(())
}
/// Sets the label for the connection
/// # Arguments
/// * `label` - The label to use for the connection
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn set_label(&self, label: impl Into<String>) {
let label_c_string = CString::new(label.into()).unwrap();
unsafe {
unsafe_bindings::restored_client_set_label(self.pointer, label_c_string.as_ptr())
};
}
}
impl Drop for RestoredClient<'_> {
fn drop(&mut self) {
unsafe {
unsafe_bindings::restored_client_free(self.pointer);
}
}
}