-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
MobileDevice.cs
executable file
·182 lines (172 loc) · 12 KB
/
MobileDevice.cs
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
namespace Manzana
{
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
internal class MobileDevice
{
private static readonly DirectoryInfo ApplicationSupportDirectory = new DirectoryInfo(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Apple Inc.\Apple Application Support", "InstallDir", Environment.CurrentDirectory).ToString());
private const string DLLName = "iTunesMobileDevice.dll";
private static readonly FileInfo iTunesMobileDeviceFile = new FileInfo(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Apple Inc.\Apple Mobile Device Support\Shared", "iTunesMobileDeviceDLL", "iTunesMobileDevice.dll").ToString());
static MobileDevice()
{
string directoryName = iTunesMobileDeviceFile.DirectoryName;
if (!iTunesMobileDeviceFile.Exists)
{
directoryName = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + @"\Apple\Mobile Device Support\bin";
if (!File.Exists(directoryName + @"\iTunesMobileDevice.dll"))
{
directoryName = @"C:\Program Files\Apple\Mobile Device Support\bin";
}
}
Environment.SetEnvironmentVariable("Path", string.Join(";", new string[] { Environment.GetEnvironmentVariable("Path"), directoryName, ApplicationSupportDirectory.FullName }));
}
[DllImport("CoreFoundation.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe void* __CFStringMakeConstantString(byte[] s);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCConnectionClose(void* conn);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCConnectionGetFSBlockSize(void* conn);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCConnectionInvalidate(void* conn);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCConnectionIsValid(void* conn);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCConnectionOpen(void* handle, uint io_timeout, ref void* conn);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCDeviceInfoOpen(void* conn, ref void* dict);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCDirectoryClose(void* conn, void* dir);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCDirectoryCreate(void* conn, string path);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCDirectoryOpen(void* conn, byte[] path, ref void* dir);
public static unsafe int AFCDirectoryOpen(void* conn, string path, ref void* dir)
{
return AFCDirectoryOpen(conn, Encoding.UTF8.GetBytes(path), ref dir);
}
public static unsafe int AFCDirectoryRead(void* conn, void* dir, ref string buffer)
{
void* dirent = null;
int num = AFCDirectoryRead(conn, dir, ref dirent);
if ((num == 0) && (dirent != null))
{
buffer = Marshal.PtrToStringAnsi(new IntPtr(dirent));
return num;
}
buffer = null;
return num;
}
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCDirectoryRead(void* conn, void* dir, ref void* dirent);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileInfoOpen(void* conn, string path, ref void* dict);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefClose(void* conn, long handle);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefOpen(void* conn, string path, int mode, int unknown, out long handle);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefRead(void* conn, long handle, byte[] buffer, ref uint len);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefSeek(void* conn, long handle, uint pos, uint origin);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefSetFileSize(void* conn, long handle, uint size);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefTell(void* conn, long handle, ref uint position);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFileRefWrite(void* conn, long handle, byte[] buffer, uint len);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCFlushData(void* conn, long handle);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCKeyValueClose(void* dict);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCKeyValueRead(void* dict, out void* key, out void* val);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCRemovePath(void* conn, string path);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AFCRenamePath(void* conn, string old_path, string new_path);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceActivate(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceConnect(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe IntPtr AMDeviceCopyDeviceIdentifier(void* device);
public static unsafe string AMDeviceCopyValue(void* device, string name)
{
IntPtr ptr = AMDeviceCopyValue_IntPtr(device, 0, CFStringMakeConstantString(name));
if (ptr != IntPtr.Zero)
{
byte len = Marshal.ReadByte(ptr, 8);
if (len > 0)
{
return Marshal.PtrToStringAnsi(new IntPtr(ptr.ToInt64() + 9L), len);
}
}
return string.Empty;
}
[DllImport("iTunesMobileDevice.dll", EntryPoint="AMDeviceCopyValue", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe IntPtr AMDeviceCopyValue_IntPtr(void* device, uint unknown, void* cfstring);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceDeactivate(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceDisconnect(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceEnterRecovery(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceGetConnectionID(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceIsPaired(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceNotificationSubscribe(DeviceNotificationCallback callback, uint unused1, uint unused2, uint unused3, out void* am_device_notification_ptr);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceRelease(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceStartService(void* device, void* service_name, ref void* handle, void* unknown);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceStartSession(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceStopSession(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMDeviceValidatePairing(void* device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern string AMRecoveryModeDeviceCopyIMEI(AMRecoveryDevice device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern string AMRecoveryModeDeviceCopySerialNumber(AMRecoveryDevice device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int AMRecoveryModeDeviceReboot(AMRecoveryDevice device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int AMRecoveryModeDeviceSetAutoBoot(AMRecoveryDevice device, byte paramByte);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern string AMRestoreModeDeviceCopyIMEI(AMRecoveryDevice device);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int AMRestoreModeDeviceCreate(uint unknown0, int connection_id, uint unknown1);
[DllImport("iTunesMobileDevice.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern unsafe int AMRestoreRegisterForDeviceNotifications(DeviceRestoreNotificationCallback dfu_connect, DeviceRestoreNotificationCallback recovery_connect, DeviceRestoreNotificationCallback dfu_disconnect, DeviceRestoreNotificationCallback recovery_disconnect, uint unknown0, void* user_info);
public static unsafe void* CFStringMakeConstantString(string s)
{
return __CFStringMakeConstantString(StringToCString(s));
}
public static string CFStringToString(byte[] value)
{
return Encoding.ASCII.GetString(value, 9, value[9]);
}
public static byte[] StringToCFString(string value)
{
byte[] bytes = new byte[value.Length + 10];
bytes[4] = 140;
bytes[5] = 7;
bytes[6] = 1;
bytes[8] = (byte) value.Length;
Encoding.ASCII.GetBytes(value, 0, value.Length, bytes, 9);
return bytes;
}
public static byte[] StringToCString(string value)
{
byte[] bytes = new byte[value.Length + 1];
Encoding.ASCII.GetBytes(value, 0, value.Length, bytes, 0);
return bytes;
}
}
}