-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
143 lines (92 loc) · 158 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Martin's TechNet Blog</title>
<subtitle>用程式碼記錄成長</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://blog.martinlau.org/"/>
<updated>2018-04-24T18:57:15.000Z</updated>
<id>http://blog.martinlau.org/</id>
<author>
<name>MartinLau</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>解析Object File Mach-O Header</title>
<link href="http://blog.martinlau.org/2018/04/PARSING-MACH-O-FILES/"/>
<id>http://blog.martinlau.org/2018/04/PARSING-MACH-O-FILES/</id>
<published>2018-04-24T13:37:42.000Z</published>
<updated>2018-04-24T18:57:15.000Z</updated>
<content type="html"><![CDATA[<blockquote><p>前言<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">本文将介绍如何解析 Mach-O Object 文件的部分 Header, 请</div><div class="line">注意这不是一个准备的指南,但希望可以给你一个帮助。</div><div class="line">更多可用的参考指南,请查阅和翻查Apple提供的文档。</div></pre></td></tr></table></figure></p></blockquote><h2 id="什么是Mach-O-文件"><a href="#什么是Mach-O-文件" class="headerlink" title="什么是Mach-O 文件"></a><strong>什么是Mach-O 文件</strong></h2><p><strong>摘自<a href="https://zh.wikipedia.org/wiki/Mach-O" target="_blank" rel="external">中文維基百科</a></strong></p><blockquote><p>Mach-O为Mach Object文件格式的缩写,它是一种用于可执行文件,目标代码,动态库,内核转储的文件格式。作为a.out格式的替代,Mach-O提供了更强的扩展性,并提升了符号表中信息的访问速度。</p><p>Mach-O曾经为大部分基于Mach核心的操作系统所使用。NeXTSTEP,Darwin和Mac OS X等系统使用这种格式作为其原生可执行文件,库和目标代码的格式。而同样使用GNU Mach作为其微内核的GNU Hurd系统则使用ELF而非Mach-O作为其标准的二进制文件格式。</p></blockquote><a id="more"></a><h2 id="Mach-O-结构"><a href="#Mach-O-结构" class="headerlink" title="Mach-O 结构"></a><strong>Mach-O 结构</strong></h2><p>典型的Mach-O文件(对应官方文档 )由三个区域組成(如下图所示):</p><ol><li>Header - 包含二进制的一般信息:字节顺序(魔数),CPU类型,加载命令的数量等。</li><li>Load commands - 这是一种目录表,它描述了段的位置,符号表,动态符号表等。每个加载命令都包含一个元信息,例如命令类型,名称,在二进制文件中的位置等等。</li><li>Data - 通常是目标文件的最大部分。 它包含代码和数据,如符号表,动态符号表等。</li></ol><p><img src="https://martin-1251734296.cos.ap-hongkong.myqcloud.com/%E8%A7%A3%E6%9E%90Object%20File%20Mach-O%20Header/WechatIMG274.jpeg" alt=""></p><p>在macOS 上有两种类型的object 文件, Mach-O 和通用二进制文件(Universal Binary),通用二进制文件又称胖二进制(Binary).他们之间区别在于Mach-O 只包含一个CPU 架构(i386,x86_64,arm64等)的目标代码,而通用二进制文件则可能包含多个CPU 架构(i386,x86_64,arm64等)的目标代码。<br><img src="https://martin-1251734296.cos.ap-hongkong.myqcloud.com/%E8%A7%A3%E6%9E%90Object%20File%20Mach-O%20Header/fat_header.png" alt=""></p><h2 id="Mach-O-Header数据结构"><a href="#Mach-O-Header数据结构" class="headerlink" title="Mach-O Header数据结构"></a><strong>Mach-O Header数据结构</strong></h2><p><strong>32位结构</strong><br><figure class="highlight h"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">struct</span> mach_header {</div><div class="line"> <span class="keyword">uint32_t</span> magic; <span class="comment">/* mach magic number identifier */</span></div><div class="line"> <span class="keyword">cpu_type_t</span> cputype; <span class="comment">/* cpu specifier */</span></div><div class="line"> <span class="keyword">cpu_subtype_t</span> cpusubtype; <span class="comment">/* machine specifier */</span></div><div class="line"> <span class="keyword">uint32_t</span> filetype; <span class="comment">/* type of file */</span></div><div class="line"> <span class="keyword">uint32_t</span> ncmds; <span class="comment">/* number of load commands */</span></div><div class="line"> <span class="keyword">uint32_t</span> sizeofcmds; <span class="comment">/* the size of all the load commands */</span></div><div class="line"> <span class="keyword">uint32_t</span> flags; <span class="comment">/* flags */</span></div><div class="line">};</div></pre></td></tr></table></figure></p><p><strong>64位结构</strong><br><figure class="highlight h"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">struct</span> mach_header_64 {</div><div class="line"> <span class="keyword">uint32_t</span> magic; <span class="comment">/* mach magic number identifier */</span></div><div class="line"> <span class="keyword">cpu_type_t</span> cputype; <span class="comment">/* cpu specifier */</span></div><div class="line"> <span class="keyword">cpu_subtype_t</span> cpusubtype; <span class="comment">/* machine specifier */</span></div><div class="line"> <span class="keyword">uint32_t</span> filetype; <span class="comment">/* type of file */</span></div><div class="line"> <span class="keyword">uint32_t</span> ncmds; <span class="comment">/* number of load commands */</span></div><div class="line"> <span class="keyword">uint32_t</span> sizeofcmds; <span class="comment">/* the size of all the load commands */</span></div><div class="line"> <span class="keyword">uint32_t</span> flags; <span class="comment">/* flags */</span></div><div class="line"> <span class="keyword">uint32_t</span> reserved; <span class="comment">/* reserved */</span></div><div class="line">};</div></pre></td></tr></table></figure></p><p><u>对比来看64位结构相较于32位结构多一个保留字段而已</u></p><blockquote><p>结构中字段解释 :</p><p><strong>magic</strong>: 魔数快速判定该文件类型</p><p><strong>cputype</strong>: CPU类型(i386,x86_64,arm6,arm64 等)</p><p><strong>cpusubtype</strong>: 对应的CPU子类型(arm64、armv7 等)</p><p><strong>filetype</strong>: 文件类型(executable、Library、Dsym等)</p><p><strong>ncmds</strong>: 加载命令数量</p><p><strong>sizeofcmds</strong>: 所有加载命令的大小</p><p><strong><u>flags</u></strong>: 文件标志</p></blockquote><h2 id="开始解析"><a href="#开始解析" class="headerlink" title="开始解析"></a><strong>开始解析</strong></h2><p>首先读取目标魔数<br><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdlib.h></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><mach-o/loader.h></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><mach-o/swap.h></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><mach-o/fat.h></span></span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dump_segments</span><span class="params">(FILE *obj_file)</span></span>;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span> </span>{</div><div class="line"> <span class="keyword">const</span> <span class="keyword">char</span> *filename = argv[<span class="number">1</span>];</div><div class="line"> FILE *obj_file = fopen(filename, <span class="string">"rb"</span>);</div><div class="line"> dump_segments(obj_file);</div><div class="line"> fclose(obj_file);</div><div class="line"></div><div class="line"> <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">uint32_t</span> read_magic(FILE *obj_file, <span class="keyword">int</span> offset) {</div><div class="line"> <span class="keyword">uint32_t</span> magic;</div><div class="line"> fseek(obj_file, offset, SEEK_SET);</div><div class="line"> fread(&magic, <span class="keyword">sizeof</span>(<span class="keyword">uint32_t</span>), <span class="number">1</span>, obj_file);</div><div class="line"> <span class="keyword">return</span> magic;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dump_segments</span><span class="params">(FILE *obj_file)</span> </span>{</div><div class="line"> <span class="keyword">uint32_t</span> magic = read_magic(obj_file, <span class="number">0</span>);</div><div class="line">}</div></pre></td></tr></table></figure></p><p>在读取目标魔数之后读取CPU 架构等讯息,但是我们知道 CPU 是分32和64架构的,这时候我们就需要检查magic了</p><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">is_magic_64</span><span class="params">(<span class="keyword">uint32_t</span> magic)</span> </span>{</div><div class="line"> <span class="keyword">return</span> magic == MH_MAGIC_64 || magic == MH_CIGAM_64;</div><div class="line">}</div></pre></td></tr></table></figure><p>由于历史原因,不同的计算机可能会使用不同的字节顺序,因此我们需要进行字节交换</p><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">should_swap_bytes</span><span class="params">(<span class="keyword">uint32_t</span> magic)</span> </span>{</div><div class="line"> <span class="keyword">return</span> magic == MH_CIGAM || magic == MH_CIGAM_64 || magic == FAT_CIGAM;</div><div class="line">}</div></pre></td></tr></table></figure><p>接下来我们就可以进行 Header 的解析了<br><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">is_fat</span><span class="params">(<span class="keyword">uint32_t</span> magic)</span> </span>{</div><div class="line"> <span class="keyword">return</span> magic == FAT_MAGIC || magic == FAT_CIGAM;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">struct</span> _cpu_type_names {</div><div class="line"> <span class="keyword">cpu_type_t</span> cputype;</div><div class="line"> <span class="keyword">const</span> <span class="keyword">char</span> *cpu_name;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">struct</span> _cpu_subtype_names {</div><div class="line"> <span class="keyword">cpu_type_t</span> cpusubtype;</div><div class="line"> <span class="keyword">const</span> <span class="keyword">char</span> *cpu_sub_name;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">struct</span> _file_type_names {</div><div class="line"> <span class="keyword">uint32_t</span> filetype;</div><div class="line"> <span class="keyword">const</span> <span class="keyword">char</span> *file_type_name;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct</span> _cpu_type_names cpu_type_names[] = {</div><div class="line"> </div><div class="line"> { CPU_TYPE_I386, <span class="string">"i386"</span> },</div><div class="line"> { CPU_TYPE_X86_64, <span class="string">"x86_64"</span> },</div><div class="line"> { CPU_TYPE_ARM, <span class="string">"ARM"</span> },</div><div class="line"> { CPU_TYPE_ARM64, <span class="string">"ARM64"</span> }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct</span> _cpu_subtype_names cpu_subtype_names[] = {</div><div class="line"> </div><div class="line"> { CPU_SUBTYPE_ARM_ALL, <span class="string">"ARM_ALL"</span> },</div><div class="line"> { CPU_SUBTYPE_ARM_V7, <span class="string">"ARM_V7"</span> },</div><div class="line"> { CPU_TYPE_ARM, <span class="string">"ARM64_ALL"</span> },</div><div class="line"> { CPU_TYPE_ARM64, <span class="string">"ARM64_V8"</span> }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct</span> _file_type_names file_type_names[] = {</div><div class="line"> </div><div class="line"> { MH_OBJECT, <span class="string">"OBJECT"</span> },</div><div class="line"> { MH_EXECUTE, <span class="string">"EXECUTE"</span> },</div><div class="line"> { MH_FVMLIB, <span class="string">"FVMLIB"</span> },</div><div class="line"> { MH_CORE, <span class="string">"CORE"</span> },</div><div class="line"> { MH_PRELOAD, <span class="string">"PRELOAD"</span> },</div><div class="line"> { MH_DYLIB, <span class="string">"DYLIB"</span> },</div><div class="line"> { MH_DYLINKER, <span class="string">"DYLINKER"</span> },</div><div class="line"> { MH_BUNDLE, <span class="string">"BUNDLE"</span> },</div><div class="line"> { MH_DYLIB_STUB, <span class="string">"DYLIB_STUB"</span> },</div><div class="line"> { MH_DSYM, <span class="string">"DSYM"</span> },</div><div class="line"> { MH_KEXT_BUNDLE, <span class="string">"KEXT_BUNDLE"</span> }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">cpu_type_name</span><span class="params">(<span class="keyword">cpu_type_t</span> cpu_type)</span> </span>{</div><div class="line"> </div><div class="line"> <span class="keyword">static</span> <span class="keyword">int</span> cpu_type_names_size = <span class="keyword">sizeof</span>(cpu_type_names) / <span class="keyword">sizeof</span>(<span class="keyword">struct</span> _cpu_type_names);</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < cpu_type_names_size; i++ ) {</div><div class="line"> <span class="keyword">if</span> (cpu_type == cpu_type_names[i].cputype) {</div><div class="line"> <span class="keyword">return</span> cpu_type_names[i].cpu_name;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="string">"unknown"</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">cpu_subtype_name</span><span class="params">(<span class="keyword">cpu_type_t</span> cpu_type)</span> </span>{</div><div class="line"> </div><div class="line"> <span class="keyword">static</span> <span class="keyword">int</span> cpu_subtype_names_size = <span class="keyword">sizeof</span>(cpu_subtype_names) / <span class="keyword">sizeof</span>(<span class="keyword">struct</span> _cpu_subtype_names);</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < cpu_subtype_names_size; i++ ) {</div><div class="line"> <span class="keyword">if</span> (cpu_type == cpu_subtype_names[i].cpusubtype) {</div><div class="line"> <span class="keyword">return</span> cpu_subtype_names[i].cpu_sub_name;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="string">"unknown"</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">file_type_name</span><span class="params">(<span class="keyword">uint32_t</span> file_type)</span> </span>{</div><div class="line"> </div><div class="line"> <span class="keyword">static</span> <span class="keyword">int</span> file_type_name_size = <span class="keyword">sizeof</span>(file_type_name) / <span class="keyword">sizeof</span>(<span class="keyword">struct</span> _file_type_names);</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < file_type_name_size; i++ ) {</div><div class="line"> <span class="keyword">if</span> (file_type == file_type_names[i].filetype) {</div><div class="line"> <span class="keyword">return</span> file_type_names[i].file_type_name;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="string">"unknown"</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> *<span class="title">load_bytes</span><span class="params">(FILE *obj_file, <span class="keyword">int</span> offset, <span class="keyword">int</span> size)</span> </span>{</div><div class="line"> <span class="keyword">void</span> *buf = <span class="built_in">calloc</span>(<span class="number">1</span>, size);</div><div class="line"> fseek(obj_file, offset, SEEK_SET);</div><div class="line"> fread(buf, size, <span class="number">1</span>, obj_file);</div><div class="line"> <span class="keyword">return</span> buf;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dump_segment_commands</span><span class="params">(FILE *obj_file, <span class="keyword">int</span> offset, <span class="keyword">int</span> is_swap, <span class="keyword">uint32_t</span> ncmds)</span> </span>{</div><div class="line"> <span class="keyword">int</span> actual_offset = offset;</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < ncmds; i++) {</div><div class="line"> <span class="keyword">struct</span> load_command *cmd = load_bytes(obj_file, actual_offset, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> load_command));</div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_load_command(cmd, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">if</span> (cmd->cmd == LC_SEGMENT_64) {</div><div class="line"> <span class="keyword">struct</span> segment_command_64 *segment = load_bytes(obj_file, actual_offset, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> segment_command_64));</div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_segment_command_64(segment, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="built_in">printf</span>(<span class="string">"segname: %s\n"</span>, segment->segname);</div><div class="line"></div><div class="line"> <span class="built_in">free</span>(segment);</div><div class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (cmd->cmd == LC_SEGMENT) {</div><div class="line"> <span class="keyword">struct</span> segment_command *segment = load_bytes(obj_file, actual_offset, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> segment_command));</div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_segment_command(segment, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="built_in">printf</span>(<span class="string">"segname: %s\n"</span>, segment->segname);</div><div class="line"></div><div class="line"> <span class="built_in">free</span>(segment);</div><div class="line"> }</div><div class="line"></div><div class="line"> actual_offset += cmd->cmdsize;</div><div class="line"></div><div class="line"> <span class="built_in">free</span>(cmd);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dump_mach_header</span><span class="params">(FILE *obj_file, <span class="keyword">int</span> offset, <span class="keyword">int</span> is_64, <span class="keyword">int</span> is_swap)</span> </span>{</div><div class="line"> <span class="keyword">uint32_t</span> ncmds;</div><div class="line"> <span class="keyword">int</span> load_commands_offset = offset;</div><div class="line"></div><div class="line"> <span class="keyword">if</span> (is_64) {</div><div class="line"> <span class="keyword">int</span> header_size = <span class="keyword">sizeof</span>(<span class="keyword">struct</span> mach_header_64);</div><div class="line"> <span class="keyword">struct</span> mach_header_64 *header = load_bytes(obj_file, offset, header_size);</div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_mach_header_64(header, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"> ncmds = header->ncmds;</div><div class="line"> load_commands_offset += header_size;</div><div class="line"></div><div class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, cpu_type_name(header->cputype));</div><div class="line"> <span class="built_in">printf</span>(<span class="string">"mach_header64_filetype: %u\n"</span>, header->filetype);</div><div class="line"></div><div class="line"> <span class="built_in">free</span>(header);</div><div class="line"></div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="keyword">int</span> header_size = <span class="keyword">sizeof</span>(<span class="keyword">struct</span> mach_header);</div><div class="line"> <span class="keyword">struct</span> mach_header *header = load_bytes(obj_file, offset, header_size);</div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_mach_header(header, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> ncmds = header->ncmds;</div><div class="line"> load_commands_offset += header_size;</div><div class="line"></div><div class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, cpu_type_name(header->cputype));</div><div class="line"> <span class="built_in">printf</span>(<span class="string">"mach_header_filetype: %u\n"</span>, header->filetype);</div><div class="line"> </div><div class="line"></div><div class="line"> <span class="built_in">free</span>(header);</div><div class="line"> }</div><div class="line"></div><div class="line"> dump_segment_commands(obj_file, load_commands_offset, is_swap, ncmds);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dump_fat_header</span><span class="params">(FILE *obj_file, <span class="keyword">int</span> is_swap)</span> </span>{</div><div class="line"> <span class="keyword">int</span> header_size = <span class="keyword">sizeof</span>(<span class="keyword">struct</span> fat_header);</div><div class="line"> <span class="keyword">int</span> arch_size = <span class="keyword">sizeof</span>(<span class="keyword">struct</span> fat_arch);</div><div class="line"></div><div class="line"> <span class="keyword">struct</span> fat_header *header = load_bytes(obj_file, <span class="number">0</span>, header_size);</div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_fat_header(header, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">int</span> arch_offset = header_size;</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < header->nfat_arch; i++) {</div><div class="line"> <span class="keyword">struct</span> fat_arch *arch = load_bytes(obj_file, arch_offset, arch_size);</div><div class="line"></div><div class="line"> <span class="keyword">if</span> (is_swap) {</div><div class="line"> swap_fat_arch(arch, <span class="number">1</span>, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">int</span> mach_header_offset = arch->offset;</div><div class="line"> <span class="built_in">free</span>(arch);</div><div class="line"> arch_offset += arch_size;</div><div class="line"></div><div class="line"> <span class="keyword">uint32_t</span> magic = read_magic(obj_file, mach_header_offset);</div><div class="line"> <span class="keyword">int</span> is_64 = is_magic_64(magic);</div><div class="line"> <span class="keyword">int</span> is_swap_mach = should_swap_bytes(magic);</div><div class="line"> dump_mach_header(obj_file, mach_header_offset, is_64, is_swap_mach);</div><div class="line"> }</div><div class="line"> <span class="built_in">free</span>(header);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dump_segments</span><span class="params">(FILE *obj_file)</span> </span>{</div><div class="line"> <span class="keyword">uint32_t</span> magic = read_magic(obj_file, <span class="number">0</span>);</div><div class="line"> <span class="keyword">int</span> is_64 = is_magic_64(magic);</div><div class="line"> <span class="keyword">int</span> is_swap = should_swap_bytes(magic);</div><div class="line"> <span class="keyword">int</span> fat = is_fat(magic);</div><div class="line"></div><div class="line"> <span class="keyword">if</span> (!fat && !is_64) {</div><div class="line"></div><div class="line"> NSLog(@<span class="string">"%@"</span>, @<span class="string">"the file is not an object file."</span>);</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">if</span> (fat) {</div><div class="line"> dump_fat_header(obj_file, is_swap);</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> dump_mach_header(obj_file, <span class="number">0</span>, is_64, is_swap);</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure></p><p>代码中有一个 fat header解析,这个本文就不做解释,具体可以参考 Apple 官方文档进行。</p><hr><h3 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a><em>参考链接</em></h3><ul><li><a href="https://link.jianshu.com/?t=http://opensource.apple.com/source/xnu/xnu-1456.1.26/EXTERNAL_HEADERS/mach-o/loader.h" target="_blank" rel="external">mach-o/loader.h</a></li><li><a href="https://www.objc.io/issues/6-build-tools/mach-o-executables/" target="_blank" rel="external">Mach-O Executables</a></li><li><a href="http://sourceforge.net/projects/machoview/" target="_blank" rel="external">MachOView</a></li><li><a href="https://archive.is/20091227035309/http://channelu.com/NeXT/NeXTStep/3.3/nd/DevTools/14_MachO/MachO.htmld/index.html#selection-837.46-837.53" target="_blank" rel="external">Mach Object Files</a></li></ul>]]></content>
<summary type="html">
<blockquote>
<p>前言<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">本文将介绍如何解析 Mach-O Object 文件的部分 Header, 请</div><div class="line">注意这不是一个准备的指南,但希望可以给你一个帮助。</div><div class="line">更多可用的参考指南,请查阅和翻查Apple提供的文档。</div></pre></td></tr></table></figure></p>
</blockquote>
<h2 id="什么是Mach-O-文件"><a href="#什么是Mach-O-文件" class="headerlink" title="什么是Mach-O 文件"></a><strong>什么是Mach-O 文件</strong></h2><p><strong>摘自<a href="https://zh.wikipedia.org/wiki/Mach-O">中文維基百科</a></strong></p>
<blockquote>
<p>Mach-O为Mach Object文件格式的缩写,它是一种用于可执行文件,目标代码,动态库,内核转储的文件格式。作为a.out格式的替代,Mach-O提供了更强的扩展性,并提升了符号表中信息的访问速度。</p>
<p>Mach-O曾经为大部分基于Mach核心的操作系统所使用。NeXTSTEP,Darwin和Mac OS X等系统使用这种格式作为其原生可执行文件,库和目标代码的格式。而同样使用GNU Mach作为其微内核的GNU Hurd系统则使用ELF而非Mach-O作为其标准的二进制文件格式。</p>
</blockquote>
</summary>
<category term="Mac 开发" scheme="http://blog.martinlau.org/categories/Mac-%E5%BC%80%E5%8F%91/"/>
<category term="Objective-C" scheme="http://blog.martinlau.org/tags/Objective-C/"/>
</entry>
<entry>
<title>iOS助手开发1-基础</title>
<link href="http://blog.martinlau.org/2017/09/iOS%E5%8A%A9%E6%89%8B%E5%BC%80%E5%8F%911-%E5%9F%BA%E7%A1%80/"/>
<id>http://blog.martinlau.org/2017/09/iOS助手开发1-基础/</id>
<published>2017-09-19T06:35:08.000Z</published>
<updated>2017-09-19T06:35:08.000Z</updated>
<content type="html"><![CDATA[<blockquote><p><strong>Hi, 太久没有写文章了,准备勤快的更新了。文章以后将开始用简体中文开始写了, 可能有词还是繁体直译,看不懂莫怪,哈哈(跑题了) 言归正传</strong></p></blockquote><hr><h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a><strong>前言</strong></h4><p>如果你厌恶iTunes带来的繁琐,却想要通过macOS来连接你的iPhone, 并和iTunes一样管理你的手机,甚至简化一些步骤,你可能需要看看这篇文章了。想要和iTunes一样连接管理iPhone装置,我们需要了解到他的private api,这可能需要你懂得反组译,我这里并不准备说明怎么反组译,你可能需要自行google it。<br><strong>so sad : (</strong><br><a id="more"></a></p><p>那么我们应该从哪里找到这个private api呢?iTunes其实也是调用了<strong>/System/Library/PrivateFrameworks/MobileDevice.framework</strong>这个库,里面包含了对iOS装置连接和通讯的函式,你可以自行研究一下反组译。<br>当然你如果太懒,就用我整理好的吧,基本我整理差不多了。<br>由于macOS基本是64bit开发了,所以我以后将以64bit做基础开发和讲解。</p><h4 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h4><blockquote><h5 id="依赖库"><a href="#依赖库" class="headerlink" title="依赖库"></a><strong>依赖库</strong></h5><ul><li>/System/Library/PrivateFrameworks/MobileDevice.framework</li><li>CoreFoundation.framework</li></ul><h5 id="开发语言"><a href="#开发语言" class="headerlink" title="开发语言"></a><strong>开发语言</strong></h5><ul><li>objective-c</li><li>c</li></ul><p><strong>以下是我反向和整理的 api-headers :</strong></p><h5 id="api-headers-Code"><a href="#api-headers-Code" class="headerlink" title="api-headers Code"></a><strong>api-headers Code</strong></h5><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div><div class="line">341</div><div class="line">342</div><div class="line">343</div><div class="line">344</div><div class="line">345</div><div class="line">346</div><div class="line">347</div><div class="line">348</div><div class="line">349</div><div class="line">350</div><div class="line">351</div><div class="line">352</div><div class="line">353</div><div class="line">354</div><div class="line">355</div><div class="line">356</div><div class="line">357</div><div class="line">358</div><div class="line">359</div><div class="line">360</div><div class="line">361</div><div class="line">362</div><div class="line">363</div><div class="line">364</div><div class="line">365</div><div class="line">366</div><div class="line">367</div><div class="line">368</div><div class="line">369</div><div class="line">370</div><div class="line">371</div><div class="line">372</div><div class="line">373</div><div class="line">374</div><div class="line">375</div><div class="line">376</div><div class="line">377</div><div class="line">378</div><div class="line">379</div><div class="line">380</div><div class="line">381</div><div class="line">382</div><div class="line">383</div><div class="line">384</div><div class="line">385</div><div class="line">386</div><div class="line">387</div><div class="line">388</div><div class="line">389</div><div class="line">390</div><div class="line">391</div><div class="line">392</div><div class="line">393</div><div class="line">394</div><div class="line">395</div><div class="line">396</div><div class="line">397</div><div class="line">398</div><div class="line">399</div><div class="line">400</div><div class="line">401</div><div class="line">402</div><div class="line">403</div><div class="line">404</div><div class="line">405</div><div class="line">406</div><div class="line">407</div><div class="line">408</div><div class="line">409</div><div class="line">410</div><div class="line">411</div><div class="line">412</div><div class="line">413</div><div class="line">414</div><div class="line">415</div><div class="line">416</div><div class="line">417</div><div class="line">418</div><div class="line">419</div><div class="line">420</div><div class="line">421</div><div class="line">422</div><div class="line">423</div><div class="line">424</div><div class="line">425</div><div class="line">426</div><div class="line">427</div><div class="line">428</div><div class="line">429</div><div class="line">430</div><div class="line">431</div><div class="line">432</div><div class="line">433</div><div class="line">434</div><div class="line">435</div><div class="line">436</div><div class="line">437</div><div class="line">438</div><div class="line">439</div><div class="line">440</div><div class="line">441</div><div class="line">442</div><div class="line">443</div><div class="line">444</div><div class="line">445</div><div class="line">446</div><div class="line">447</div><div class="line">448</div><div class="line">449</div><div class="line">450</div><div class="line">451</div><div class="line">452</div><div class="line">453</div><div class="line">454</div><div class="line">455</div><div class="line">456</div><div class="line">457</div><div class="line">458</div><div class="line">459</div><div class="line">460</div><div class="line">461</div><div class="line">462</div><div class="line">463</div><div class="line">464</div><div class="line">465</div><div class="line">466</div><div class="line">467</div><div class="line">468</div><div class="line">469</div><div class="line">470</div><div class="line">471</div><div class="line">472</div><div class="line">473</div><div class="line">474</div><div class="line">475</div><div class="line">476</div><div class="line">477</div><div class="line">478</div><div class="line">479</div><div class="line">480</div><div class="line">481</div><div class="line">482</div><div class="line">483</div><div class="line">484</div><div class="line">485</div><div class="line">486</div><div class="line">487</div><div class="line">488</div><div class="line">489</div><div class="line">490</div><div class="line">491</div><div class="line">492</div><div class="line">493</div><div class="line">494</div><div class="line">495</div><div class="line">496</div><div class="line">497</div><div class="line">498</div><div class="line">499</div><div class="line">500</div><div class="line">501</div><div class="line">502</div><div class="line">503</div><div class="line">504</div><div class="line">505</div><div class="line">506</div><div class="line">507</div><div class="line">508</div><div class="line">509</div><div class="line">510</div><div class="line">511</div><div class="line">512</div><div class="line">513</div><div class="line">514</div><div class="line">515</div><div class="line">516</div><div class="line">517</div><div class="line">518</div><div class="line">519</div><div class="line">520</div><div class="line">521</div><div class="line">522</div><div class="line">523</div><div class="line">524</div><div class="line">525</div><div class="line">526</div><div class="line">527</div><div class="line">528</div><div class="line">529</div><div class="line">530</div><div class="line">531</div><div class="line">532</div><div class="line">533</div><div class="line">534</div><div class="line">535</div><div class="line">536</div><div class="line">537</div><div class="line">538</div><div class="line">539</div><div class="line">540</div><div class="line">541</div><div class="line">542</div><div class="line">543</div><div class="line">544</div><div class="line">545</div><div class="line">546</div><div class="line">547</div><div class="line">548</div><div class="line">549</div><div class="line">550</div><div class="line">551</div><div class="line">552</div><div class="line">553</div><div class="line">554</div><div class="line">555</div><div class="line">556</div><div class="line">557</div><div class="line">558</div><div class="line">559</div><div class="line">560</div><div class="line">561</div><div class="line">562</div><div class="line">563</div><div class="line">564</div><div class="line">565</div><div class="line">566</div><div class="line">567</div><div class="line">568</div><div class="line">569</div><div class="line">570</div><div class="line">571</div><div class="line">572</div><div class="line">573</div><div class="line">574</div><div class="line">575</div><div class="line">576</div><div class="line">577</div><div class="line">578</div><div class="line">579</div><div class="line">580</div><div class="line">581</div><div class="line">582</div><div class="line">583</div><div class="line">584</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> __cplusplus</span></div><div class="line"><span class="keyword">extern</span> <span class="string">"C"</span> {</div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div><div class="line"> </div><div class="line"> <span class="comment">/*</span></div><div class="line"> * its critical that we spell out all integer sizes, for the entry points</div><div class="line"> * and data structures in the external DLL/framework that we have no</div><div class="line"> * control over.</div><div class="line"> */</div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdint.h></span></span></div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">if</span> defined(WIN32)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><CoreFoundation.h></span></span></div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">uint32_t</span> kAMDError;</div><div class="line"><span class="meta">#<span class="meta-keyword">elif</span> defined(__APPLE__)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><CoreFoundation/CoreFoundation.h></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><mach/error.h></span></span></div><div class="line"> </div><div class="line"><span class="meta">#import <span class="meta-string">"MSMobileDeviceMacro.h"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* Error codes */</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_APPLE_MOBILE(err_system(0x3a))</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_IPHONE(err_sub(0))</span></div><div class="line"> </div><div class="line"> <span class="comment">/* Apple Mobile (AM*) errors */</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_OK ERR_SUCCESS</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_SYSCALL (ERR_MOBILE_DEVICE | 0x01)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_OUT_OF_MEMORY (ERR_MOBILE_DEVICE | 0x03)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_QUERY_FAILED (ERR_MOBILE_DEVICE | 0x04)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_INVALID_ARGUMENT (ERR_MOBILE_DEVICE | 0x0b)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_NO_SUCH_SERVICE (ERR_MOBILE_DEVICE | 0x22)</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_DICT_NOT_LOADED (ERR_MOBILE_DEVICE | 0x25)</span></div><div class="line"> </div><div class="line"> <span class="comment">/* Apple File Connection (AFC*) errors */</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MDERR_AFC_OUT_OF_MEMORY 0x03</span></div><div class="line"> </div><div class="line"> <span class="comment">/* Services, found in /System/Library/Lockdown/Services.plist */</span></div><div class="line"> <span class="comment">//com.apple.springboardservices</span></div><div class="line"> <span class="comment">//com.apple.mobile.installation_proxy</span></div><div class="line"> <span class="comment">//com.apple.mobile.house_arrest</span></div><div class="line"> <span class="comment">//com.apple.crashreportcopymover</span></div><div class="line"> <span class="comment">//com.apple.crashreportcopymobile</span></div><div class="line"> <span class="comment">//com.apple.misagent</span></div><div class="line"> <span class="comment">//com.apple.debug_image_mount</span></div><div class="line"> <span class="comment">//com.apple.mobile.integrity_relay</span></div><div class="line"> <span class="comment">//com.apple.mobile.MCInstall</span></div><div class="line"> <span class="comment">//com.apple.mobile.mobile_image_mounter</span></div><div class="line"> <span class="comment">//com.apple.mobile.diagnostics_relay</span></div><div class="line"> <span class="comment">//com.apple.mobile.factory_proxy</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_ATC <span class="meta-string">"com.apple.atc"</span> <span class="comment">//同步服务</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_AFC <span class="meta-string">"com.apple.afc"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_AFC2 <span class="meta-string">"com.apple.afc2"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_BACKUP <span class="meta-string">"com.apple.mobilebackup"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_CRASH_REPORT_COPY <span class="meta-string">"com.apple.crashreportcopy"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_DEBUG_IMAGE_MOUNT <span class="meta-string">"com.apple.mobile.debug_image_mount"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_NOTIFICATION_PROXY <span class="meta-string">"com.apple.mobile.notification_proxy"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_PURPLE_TEST <span class="meta-string">"com.apple.purpletestr"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SOFTWARE_UPDATE <span class="meta-string">"com.apple.mobile.software_update"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SYNC <span class="meta-string">"com.apple.mobilesync"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SCREENSHOT <span class="meta-string">"com.apple.screenshotr"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SYSLOG_RELAY <span class="meta-string">"com.apple.syslog_relay"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SYSTEM_PROFILER <span class="meta-string">"com.apple.mobile.system_profiler"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SCREENSHOTR <span class="meta-string">"com.apple.mobile.screenshotr"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_FILE_RELAY <span class="meta-string">"com.apple.mobile.file_relay"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_HOUSE_ARREST <span class="meta-string">"com.apple.mobile.house_arrest"</span> <span class="comment">//应用内文件服务</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_SPRINGBOARD_SERVICE <span class="meta-string">"com.apple.springboardservices"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> AMSVC_INSTALLATION_PROXY <span class="meta-string">"com.apple.mobile.installation_proxy"</span></span></div><div class="line"> </div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">// Apple's opaque types</span></div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">uint32_t</span> <span class="keyword">afc_error_t</span>;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">uint64_t</span> afc_file_ref;</div><div class="line"> </div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">int</span> am_service;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">int</span> am_connection;</div><div class="line"> </div><div class="line"> <span class="comment">/* opaque structures */</span></div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _am_device*am_device;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _am_restore_device*am_restore_device;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _am_recovery_device*am_recovery_device;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _afc_connection*afc_connection;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _am_device_notification*am_device_notification;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _afc_directory*afc_directory;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _afc_dictionary*afc_dictionary;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> _afc_operation*afc_operation;</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">void</span> * ath_connection;</div><div class="line"> </div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">struct</span> ssl_st *SSL;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">union</span> _kAMDUnionRef</div><div class="line"> {</div><div class="line"> <span class="keyword">long</span> aaaa;</div><div class="line"> <span class="keyword">char</span> bbb[<span class="number">8</span>];</div><div class="line"> } kAMDUnionRef;</div><div class="line"> </div><div class="line"> <span class="comment">/* Messages passed to device notification callbacks: passed as part of</span></div><div class="line"> * am_device_notification_callback_info. */</div><div class="line"> <span class="keyword">typedef</span> <span class="keyword">enum</span> {</div><div class="line"> ADNCI_MSG_CONNECTED = <span class="number">1</span>,</div><div class="line"> ADNCI_MSG_DISCONNECTED = <span class="number">2</span>,</div><div class="line"> ADNCI_MSG_UNSUBSCRIBED = <span class="number">3</span></div><div class="line"> } adnci_msg;</div><div class="line"> </div><div class="line"> <span class="keyword">struct</span> am_device_notification_callback_info {</div><div class="line"> am_devicedev;<span class="comment">/* 0 device */</span></div><div class="line"> <span class="keyword">uint32_t</span>msg;<span class="comment">/* 4 one of adnci_msg */</span></div><div class="line"> } __attribute__ ((packed));</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* The type of the device notification callback function. */</span></div><div class="line"> <span class="function"><span class="keyword">typedef</span> <span class="title">void</span> <span class="params">(*am_device_notification_callback)</span> <span class="params">(<span class="keyword">struct</span> am_device_notification_callback_info *, <span class="keyword">void</span>* callback_data)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/* The type of the device restore notification callback functions. */</span></div><div class="line"> <span class="function"><span class="keyword">typedef</span> <span class="title">void</span> <span class="params">(*am_restore_device_notification_callback)</span><span class="params">(am_recovery_device *)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> mark - Device</span></div><div class="line"> </div><div class="line"> <span class="comment">/* ----------------------------------------------------------------------------</span></div><div class="line"> * Public routines</div><div class="line"> * ------------------------------------------------------------------------- */</div><div class="line"> </div><div class="line"> <span class="comment">/* Registers a notification with the current run loop. The callback gets</span></div><div class="line"> * copied into the notification struct, as well as being registered with the</div><div class="line"> * current run loop. callback_data gets passed to the callback in addition</div><div class="line"> * to the info block.</div><div class="line"> * unused0 and unused1 are both 0 when iTunes calls this.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> * MDERR_SYSCALL if CFRunLoopAddSource() failed</div><div class="line"> * MDERR_OUT_OF_MEMORY if we ran out of memory</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceNotificationSubscribe</span><span class="params">(am_device_notification_callback callback,</span></span></div><div class="line"> <span class="keyword">uint32_t</span> unused0,</div><div class="line"> <span class="keyword">uint32_t</span> unused1,</div><div class="line"> <span class="keyword">void</span> *callback_data,</div><div class="line"> am_device_notification *notification);</div><div class="line"> </div><div class="line"> <span class="comment">/*</span></div><div class="line"> * Unregisters notifications. Buggy (iTunes 8.2): if you subscribe, unsubscribe and subscribe again, arriving</div><div class="line"> notifications will contain cookie and subscription from 1st call to subscribe, not the 2nd one. iTunes</div><div class="line"> calls this function only once on exit.</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceNotificationUnsubscribe</span><span class="params">(am_device_notification subscription)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* Registers for device notifications related to the restore process. unknown0</span></div><div class="line"> * is zero when iTunes calls this. In iTunes,</div><div class="line"> * the callbacks are located at:</div><div class="line"> * 1: $3ac68e-$3ac6b1, calls $3ac542(unknown1, arg, 0)</div><div class="line"> * 2: $3ac66a-$3ac68d, calls $3ac542(unknown1, 0, arg)</div><div class="line"> * 3: $3ac762-$3ac785, calls $3ac6b2(unknown1, arg, 0)</div><div class="line"> * 4: $3ac73e-$3ac761, calls $3ac6b2(unknown1, 0, arg)</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMRestoreRegisterForDeviceNotifications</span><span class="params">(am_restore_device_notification_callback dfu_connect_callback, am_restore_device_notification_callback recovery_connect_callback, am_restore_device_notification_callback dfu_disconnect_callback, am_restore_device_notification_callback recovery_disconnect_callback, <span class="keyword">unsigned</span> <span class="keyword">int</span> unknown0, <span class="keyword">void</span> *user_info)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/* Connects to the iPhone. Pass in the am_device structure that the</span></div><div class="line"> * notification callback will give to you.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successfully connected</div><div class="line"> * MDERR_SYSCALL if setsockopt() failed</div><div class="line"> * MDERR_QUERY_FAILED if the daemon query failed</div><div class="line"> * MDERR_INVALID_ARGUMENT if USBMuxConnectByPort returned 0xffffffff</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceConnect</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceDisconnect</span><span class="params">(am_device device)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceRetain</span><span class="params">(am_device device)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceRelease</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">// __DLLIMPORT kAMDError AMDevicePair(struct am_device device);</span></div><div class="line"> <span class="function">kAMDError <span class="title">AMDevicePair</span><span class="params">(am_device device)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceUnpair</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/* Calls PairingRecordPath() on the given device, than tests whether the path</span></div><div class="line"> * which that function returns exists. During the initial connect, the path</div><div class="line"> * returned by that function is '/', and so this returns 1.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * 0 if the path did not exist</div><div class="line"> * 1 if it did</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">AMDeviceIsPaired</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* iTunes calls this function immediately after testing whether the device is</span></div><div class="line"> * paired. It creates a pairing file and establishes a Lockdown connection.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> * MDERR_INVALID_ARGUMENT if the supplied device is null</div><div class="line"> * MDERR_DICT_NOT_LOADED if the load_dict() call failed</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceValidatePairing</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* Creates a Lockdown session and adjusts the device structure appropriately</span></div><div class="line"> * to indicate that the session has been started. iTunes calls this function</div><div class="line"> * after validating pairing.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> * MDERR_INVALID_ARGUMENT if the Lockdown conn has not been established</div><div class="line"> * MDERR_DICT_NOT_LOADED if the load_dict() call failed</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceStartSession</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* Starts a service and returns a handle that can be used in order to further</span></div><div class="line"> * access the service. You should stop the session and disconnect before using</div><div class="line"> * the service. iTunes calls this function after starting a session. It starts</div><div class="line"> * the service and the SSL connection. unknown may safely be</div><div class="line"> * NULL (it is when iTunes calls this), but if it is not, then it will be</div><div class="line"> * filled upon function exit. service_name should be one of the AMSVC_*</div><div class="line"> * constants. If the service is AFC (AMSVC_AFC), then the handle is the handle</div><div class="line"> * that will be used for further AFC* calls.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> * MDERR_SYSCALL if the setsockopt() call failed</div><div class="line"> * MDERR_INVALID_ARGUMENT if the Lockdown conn has not been established</div><div class="line"> */</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceStartService</span><span class="params">(am_device device,</span></span></div><div class="line"> CFStringRef service_name,</div><div class="line"> am_service *handle,</div><div class="line"> <span class="keyword">uint32_t</span> *unknown);</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceSecureStartService</span><span class="params">(am_device device,</span></span></div><div class="line"> CFStringRef service_name,</div><div class="line"> am_service *handle,</div><div class="line"> am_connection *securitySocketCookie);</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">AMDServiceConnectionGetSocket</span><span class="params">(am_connection handle)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/* Start and Stops a session. You should do this before accessing services.</span></div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> * MDERR_INVALID_ARGUMENT if the Lockdown conn has not been established</div><div class="line"> */</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceStartSession</span><span class="params">(am_device device)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceStopSession</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AMDeviceGetConnectionID(am_device device);</div><div class="line"> <span class="keyword">uint32_t</span> AMDeviceEnterRecovery(am_device device);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AMDeviceGetInterfaceType(am_device device); <span class="comment">// { return 1; //USB return 2; //iTuens_WIFI }</span></div><div class="line"> <span class="keyword">uint32_t</span> AMDeviceGetInterfaceSpeed(am_device device); <span class="comment">// { return 0x78000; } //废弃</span></div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* Reads various device settings; returns nil if no value is found for</span></div><div class="line"> * the nominated key</div><div class="line"> *</div><div class="line"> * Must be balanced by CFRelease()</div><div class="line"> *</div><div class="line"> * Possible values for key:</div><div class="line"> * ActivationState</div><div class="line"> * ActivationStateAcknowledged</div><div class="line"> * BasebandBootloaderVersion</div><div class="line"> * BasebandVersion</div><div class="line"> * BluetoothAddress</div><div class="line"> * BuildVersion</div><div class="line"> * DeviceCertificate</div><div class="line"> * DeviceClass</div><div class="line"> * DeviceName</div><div class="line"> * DevicePublicKey</div><div class="line"> * FirmwareVersion</div><div class="line"> * HostAttached</div><div class="line"> * IntegratedCircuitCardIdentity</div><div class="line"> * InternationalMobileEquipmentIdentity</div><div class="line"> * InternationalMobileSubscriberIdentity</div><div class="line"> * ModelNumber</div><div class="line"> * PhoneNumber</div><div class="line"> * ProductType</div><div class="line"> * ProductVersion</div><div class="line"> * ProtocolVersion</div><div class="line"> * RegionInfo</div><div class="line"> * SBLockdownEverRegisteredKey</div><div class="line"> * SIMStatus</div><div class="line"> * SerialNumber</div><div class="line"> * SomebodySetTimeZone</div><div class="line"> * TimeIntervalSince1970</div><div class="line"> * TimeZone</div><div class="line"> * TimeZoneOffsetFromUTC</div><div class="line"> * TrustedHostAttached</div><div class="line"> * UniqueDeviceID</div><div class="line"> * Uses24HourClock</div><div class="line"> * WiFiAddress</div><div class="line"> * iTunesHasConnectedAMDeviceSetWirelessBuddyFlags</div><div class="line"> */</div><div class="line"> <span class="function">CFTypeRef <span class="title">AMDeviceCopyValue</span><span class="params">(am_device device, CFStringRef domain, CFStringRef key)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceSetValue</span><span class="params">(am_device device, CFStringRef domain, CFStringRef key, CFTypeRef value)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceRemoveValue</span><span class="params">(am_device device, CFStringRef domain, CFStringRef key)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/*</span></div><div class="line"> * Returns the udid of the specified device. The same value is returned</div><div class="line"> * from AMDeviceCopyValue(device,0,"UniqueDeviceID").</div><div class="line"> *</div><div class="line"> * Must be balanced by CFRelease()</div><div class="line"> */</div><div class="line"> <span class="function">CFStringRef <span class="title">AMDeviceCopyDeviceIdentifier</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/*edits by geohot*/</span></div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceDeactivate</span><span class="params">(am_device device)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceActivate</span><span class="params">(am_device device, CFMutableDictionaryRef)</span></span>;</div><div class="line"> <span class="comment">/*end*/</span></div><div class="line"> </div><div class="line"><span class="comment">// kAMDError AMDeviceProxyGetECID(am_device device);</span></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">AMDDeviceDetached</span><span class="params">(am_device device)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">SSL *<span class="title">AMDServiceConnectionGetSecureIOContext</span><span class="params">(am_connection conn)</span></span>;</div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> mark - RecoveryDevice</span></div><div class="line"> </div><div class="line"> <span class="comment">/* Causes the restore functions to spit out (unhelpful) progress messages to</span></div><div class="line"> * the file specified by the given path. iTunes always calls this right before</div><div class="line"> * restoring with a path of</div><div class="line"> * "$HOME/Library/Logs/iPhone Updater Logs/iPhoneUpdater X.log", where X is an</div><div class="line"> * unused number.</div><div class="line"> */</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="title">AMRestoreEnableFileLogging</span><span class="params">(<span class="keyword">char</span> *path)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/* Initializes a new option dictionary to default values. Pass the constant</span></div><div class="line"> * kCFAllocatorDefault as the allocator. The option dictionary looks as</div><div class="line"> * follows:</div><div class="line"> * {</div><div class="line"> * NORImageType => 'production',</div><div class="line"> * AutoBootDelay => 0,</div><div class="line"> * KernelCacheType => 'Release',</div><div class="line"> * UpdateBaseband => true,</div><div class="line"> * DFUFileType => 'RELEASE',</div><div class="line"> * SystemImageType => 'User',</div><div class="line"> * CreateFilesystemPartitions => true,</div><div class="line"> * FlashNOR => true,</div><div class="line"> * RestoreBootArgs => 'rd=md0 nand-enable-reformat=1 -progress'</div><div class="line"> * BootImageType => 'User'</div><div class="line"> * }</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * the option dictionary if successful</div><div class="line"> * NULL if out of memory</div><div class="line"> */</div><div class="line"> </div><div class="line"> <span class="function">CFMutableDictionaryRef <span class="title">AMRestoreCreateDefaultOptions</span><span class="params">(CFAllocatorRef allocator)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMRestorePerformRecoveryModeRestore</span><span class="params">(am_recovery_device *rdev, CFDictionaryRef opts, <span class="keyword">void</span> *callback, <span class="keyword">void</span> *user_info)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMRestorePerformRestoreModeRestore</span><span class="params">(am_restore_device *rdev, CFDictionaryRef opts, <span class="keyword">void</span> *callback, <span class="keyword">void</span> *user_info)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">am_restore_device *<span class="title">AMRestoreModeDeviceCreate</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">int</span> unknown0, <span class="keyword">unsigned</span> <span class="keyword">int</span> connection_id, <span class="keyword">unsigned</span> <span class="keyword">int</span> unknown1)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMRestoreCreatePathsForBundle</span><span class="params">(CFStringRef restore_bundle_path, CFStringRef kernel_cache_type, CFStringRef boot_image_type, <span class="keyword">unsigned</span> <span class="keyword">int</span> unknown0, CFStringRef *firmware_dir_path, CFStringRef *kernelcache_restore_path, <span class="keyword">unsigned</span> <span class="keyword">int</span> unknown1, CFStringRef *ramdisk_path)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMRecoveryModeDeviceSetAutoBoot</span><span class="params">(am_recovery_device *rdev, CFStringRef state, <span class="keyword">int</span> unknown1, <span class="keyword">int</span> unknown2, <span class="keyword">int</span> unknown3)</span></span>;</div><div class="line"> <span class="function">kAMDError <span class="title">AMRecoveryModeDeviceReboot</span><span class="params">(am_recovery_device *rdev, CFStringRef state, <span class="keyword">int</span> unknown1, <span class="keyword">int</span> unknown2, <span class="keyword">int</span> unknown3)</span></span>;</div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> mark - MessageOperate</span></div><div class="line"> </div><div class="line"> <span class="keyword">kern_return_t</span> AMDeviceSendMessage(am_service socket, <span class="keyword">void</span> *unused, CFPropertyListRef plist);</div><div class="line"> <span class="keyword">kern_return_t</span> AMDeviceReceiveMessage(am_service socket, CFDictionaryRef options, CFPropertyListRef * result);</div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> mark - Application</span></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">typedef</span> <span class="title">void</span> <span class="params">(*InstallationProxyCallBack)</span><span class="params">(CFDictionaryRef dict, <span class="keyword">void</span> *arg)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceLookupApplicationArchives</span><span class="params">(am_device device, CFDictionaryRef optionDict, CFDictionaryRef *archives)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceLookupApplications</span><span class="params">(am_device device, CFDictionaryRef optionDict, CFDictionaryRef *appList)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceRemoveApplicationArchive</span><span class="params">(am_service afcSock, CFStringRef cfBundleId, CFDictionaryRef optionDict, InstallationProxyCallBack callbackBlock, <span class="keyword">void</span> *unknown)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceUninstallApplication</span><span class="params">(am_service afcSock, CFStringRef cfBundleId, CFDictionaryRef optionDict, InstallationProxyCallBack callbackBlock, <span class="keyword">void</span> *unknown)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceInstallApplication</span><span class="params">(am_service installProxySock, CFStringRef cfStringPath, CFDictionaryRef optionDict, InstallationProxyCallBack installCallback, <span class="keyword">void</span> *unknow)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceSecureTransferPath</span><span class="params">(am_service afcSock, am_device device, CFStringRef cfStringPath, CFDictionaryRef optionDict, InstallationProxyCallBack transferCallback, <span class="keyword">void</span> *delegate)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDeviceTransferApplication</span><span class="params">(am_service afcSock, CFStringRef cfStringPath, CFDictionaryRef optionDict, InstallationProxyCallBack transferCallback, <span class="keyword">void</span> *unknown)</span></span>;</div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> mark - Notification stuff</span></div><div class="line"> </div><div class="line"> <span class="comment">// Notification stuff - only call these on "com.apple.mobile.notification_proxy" (AMSVC_NOTIFICATION_PROXY)</span></div><div class="line"> <span class="function"><span class="keyword">typedef</span> <span class="title">void</span> <span class="params">(*NOTIFY_CALLBACK)</span><span class="params">(CFStringRef notification, <span class="keyword">void</span>* data)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDPostNotification</span><span class="params">(am_service socket, CFStringRef notification, CFStringRef userinfo)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDObserveNotification</span><span class="params">(am_service socket, CFStringRef notification)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDListenForNotifications</span><span class="params">(am_service socket, NOTIFY_CALLBACK cb, <span class="keyword">void</span>* data)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AMDShutdownNotificationProxy</span><span class="params">(am_service socket)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> mark - AFC connection functions</span></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">const</span> <span class="keyword">char</span> * <span class="title">AFCGetClientVersionString</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">// "@(#)PROGRAM:afc PROJECT:afc-80"</span></div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCConnectionOpen(am_service handle,<span class="keyword">uint32_t</span> io_timeout,afc_connection *conn);</div><div class="line"> <span class="keyword">afc_error_t</span> AFCConnectionClose(afc_connection conn);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionGetContext(afc_connection conn);</div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionSetContext(afc_connection conn, <span class="keyword">uint32_t</span> ctx);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionGetContext(afc_connection conn);</div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionSetContext(afc_connection conn, <span class="keyword">uint32_t</span> ctx);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionGetFSBlockSize(afc_connection conn);</div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionSetFSBlockSize(afc_connection conn, <span class="keyword">uint32_t</span> size);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionGetIOTimeout(afc_connection conn);</div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionSetIOTimeout(afc_connection conn, <span class="keyword">uint32_t</span> timeout);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionGetSocketBlockSize(afc_connection conn);</div><div class="line"> <span class="keyword">uint32_t</span> AFCConnectionSetSocketBlockSize(afc_connection conn, <span class="keyword">uint32_t</span> size);</div><div class="line"> </div><div class="line"> <span class="function">kAMDError <span class="title">AFCConnectionSetSecureContext</span><span class="params">(afc_connection conn, SSL *context)</span></span>;</div><div class="line"> </div><div class="line"> <span class="function">CFStringRef <span class="title">AFCCopyErrorString</span><span class="params">(afc_connection a)</span></span>;</div><div class="line"> <span class="function">CFTypeRef <span class="title">AFCConnectionCopyLastErrorInfo</span><span class="params">(afc_connection a)</span></span>;</div><div class="line"> </div><div class="line"> </div><div class="line"> <span class="comment">/* Opens an Apple File Connection. You must start the appropriate service</span></div><div class="line"> * first with AMDeviceStartService(). In iTunes, io_timeout is 0.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> * MDERR_AFC_OUT_OF_MEMORY if malloc() failed</div><div class="line"> */</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCDirectoryAccessOpen(am_service handle,</div><div class="line"> <span class="keyword">uint32_t</span> io_timeout,</div><div class="line"> afc_connection *conn);</div><div class="line"> </div><div class="line"> <span class="comment">/* Retrieves an afc_dictionary that describes the connected device. To</span></div><div class="line"> * extract values from the dictionary, use AFCKeyValueRead() and close</div><div class="line"> * it when finished with AFCKeyValueClose()</div><div class="line"> */</div><div class="line"> <span class="keyword">afc_error_t</span> AFCDeviceInfoOpen(afc_connection conn,</div><div class="line"> afc_dictionary *info);</div><div class="line"> </div><div class="line"> <span class="comment">/* Turns debug mode on if the environment variable AFCDEBUG is set to a numeric</span></div><div class="line"> * value, or if the file '/AFCDEBUG' is present and contains a value. */</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">AFCPlatformInit</span><span class="params">()</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">/* Opens a directory on the iPhone. Retrieves an afc_directory which can be</span></div><div class="line"> * queried (with AFCDirectoryRead()) to enumerate through the filenames in</div><div class="line"> * the directory. Once finished, close with AFCDirectoryClose()</div><div class="line"> *</div><div class="line"> * Note that this normally only accesses the iTunes sandbox/partition as the</div><div class="line"> * root, which is /var/root/Media. Pathnames are specified with '/' delimiters</div><div class="line"> * as in Unix style.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful</div><div class="line"> */</div><div class="line"> <span class="keyword">afc_error_t</span> AFCDirectoryOpen(afc_connection conn,<span class="keyword">const</span> <span class="keyword">char</span> *path,afc_directory *dir);</div><div class="line"> </div><div class="line"> <span class="comment">/* Acquires the next entry in a directory previously opened with</span></div><div class="line"> * AFCDirectoryOpen(). When dirent is filled with a NULL value, then the end</div><div class="line"> * of the directory has been reached. '.' and '..' will be returned as the</div><div class="line"> * first two entries in each directory except the root; you may want to skip</div><div class="line"> * over them.</div><div class="line"> *</div><div class="line"> * Returns:</div><div class="line"> * MDERR_OK if successful, even if no entries remain</div><div class="line"> */</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCDirectoryRead(afc_connection conn,</div><div class="line"> afc_directory dir,</div><div class="line"> <span class="keyword">char</span> **dirent);</div><div class="line"> </div><div class="line"> <span class="comment">/* Close the directory previously opened with AFCDirectoryOpen()</span></div><div class="line"> */</div><div class="line"> <span class="keyword">afc_error_t</span> AFCDirectoryClose(afc_connection conn,</div><div class="line"> afc_directory dir);</div><div class="line"> </div><div class="line"> <span class="comment">/* Create a new directory on the device.</span></div><div class="line"> */</div><div class="line"> <span class="keyword">afc_error_t</span> AFCDirectoryCreate(afc_connection conn, <span class="keyword">const</span> <span class="keyword">char</span> *dirname);</div><div class="line"> </div><div class="line"> <span class="comment">/* Removes an existing file or directory from the device.</span></div><div class="line"> */</div><div class="line"> <span class="keyword">afc_error_t</span> AFCRemovePath(afc_connection conn, <span class="keyword">const</span> <span class="keyword">char</span> *dirname);</div><div class="line"> </div><div class="line"> <span class="comment">/* Renames an existing file or directory on the device */</span></div><div class="line"> <span class="keyword">afc_error_t</span> AFCRenamePath(afc_connection conn, <span class="keyword">const</span> <span class="keyword">char</span> *from, <span class="keyword">const</span> <span class="keyword">char</span> *to);</div><div class="line"> </div><div class="line"> <span class="comment">/* Returns the context field of the given AFC connection. */</span></div><div class="line"> <span class="keyword">uint32_t</span> AFCDirectoryAccessGetContext(afc_connection conn);</div><div class="line"> </div><div class="line"> <span class="comment">/* Set the context field of the given AFC connection.</span></div><div class="line"> */</div><div class="line"> <span class="keyword">uint32_t</span> AFCDirectoryAccessSetContext(afc_connection conn, <span class="keyword">uint32_t</span> ctx);</div><div class="line"> </div><div class="line"> <span class="comment">/* Returns the fs_block_size field of the given AFC connection. */</span></div><div class="line"> <span class="keyword">uint32_t</span> AFCDirectoryAccessGetFSBlockSize(afc_connection conn);</div><div class="line"> </div><div class="line"> <span class="comment">/* Returns the io_timeout field of the given AFC connection. In iTunes this is 0. */</span></div><div class="line"> <span class="keyword">uint32_t</span> AFCDirectoryAccessGetIOTimeout(afc_connection conn);</div><div class="line"> </div><div class="line"> <span class="keyword">uint32_t</span> AFCDirectoryAccessSetIOTimeout(afc_connection conn, <span class="keyword">uint32_t</span> timeout);</div><div class="line"> </div><div class="line"> <span class="comment">/* Returns the sock_block_size field of the given AFC connection. */</span></div><div class="line"> <span class="keyword">uint32_t</span> AFCDirectoryAccessGetSocketBlockSize(afc_connection conn);</div><div class="line"> </div><div class="line"> <span class="comment">/* Closes the given AFC connection. */</span></div><div class="line"> <span class="keyword">afc_error_t</span> AFCDirectoryAccessClose(afc_connection conn);</div><div class="line"> </div><div class="line"> <span class="comment">/* ----------------------------------------------------------------------------</span></div><div class="line"> * Less-documented public routines</div><div class="line"> * ------------------------------------------------------------------------- */</div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefOpen(afc_connection conn,</div><div class="line"> <span class="keyword">const</span> <span class="keyword">char</span> *path,<span class="comment">/* pathname of file to open */</span></div><div class="line"> <span class="keyword">uint64_t</span> mode,<span class="comment">/* 1=read, 2=write, 3=read/write */</span></div><div class="line"> afc_file_ref *ref);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefSeek(afc_connection conn,</div><div class="line"> afc_file_ref ref,</div><div class="line"> <span class="keyword">int64_t</span> offset,<span class="comment">/* signed offset from pos */</span></div><div class="line"> <span class="keyword">uint64_t</span> pos);<span class="comment">/* 0=SEEK_SET, 1=SEEK_CUR, 2=SEEK_END */</span></div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefTell(afc_connection conn,</div><div class="line"> afc_file_ref ref,</div><div class="line"> <span class="keyword">uint64_t</span> *offset);</div><div class="line"> </div><div class="line"> <span class="comment">// afc_error_t AFCFileRefLock(afc_connection *conn, afc_file_ref ref,</span></div><div class="line"> <span class="comment">// ...);</span></div><div class="line"> <span class="comment">// int _AFCDirectoryAccessIsValid(afc_connection *conn)</span></div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefRead(afc_connection conn, afc_file_ref ref, <span class="keyword">void</span> *buf, <span class="keyword">uint32_t</span> *len);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefSetFileSize(afc_connection conn, afc_file_ref ref, <span class="keyword">uint64_t</span> offset);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefWrite(afc_connection conn, afc_file_ref ref, <span class="keyword">const</span> <span class="keyword">void</span> *buf, <span class="keyword">uint32_t</span> len);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileRefClose(afc_connection conn, afc_file_ref ref);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCFileInfoOpen(afc_connection conn, <span class="keyword">const</span> <span class="keyword">char</span> *path, afc_dictionary *info);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCKeyValueRead(afc_dictionary dict, <span class="keyword">char</span> **key, <span class="keyword">char</span> **val);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCKeyValueClose(afc_dictionary dict);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCLinkPath(afc_connection conn,<span class="keyword">uint64_t</span> mode, <span class="keyword">const</span> <span class="keyword">char</span> *target,<span class="keyword">const</span> <span class="keyword">char</span> *link);</div><div class="line"> </div><div class="line"> <span class="keyword">afc_error_t</span> AFCConnectionProcessOperation(afc_connection a1, afc_operation op, <span class="keyword">double</span> timeout);</div><div class="line"> <span class="keyword">afc_error_t</span> AFCOperationGetResultStatus(afc_operation op);</div><div class="line"> <span class="function">CFTypeRef <span class="title">AFCOperationGetResultObject</span><span class="params">(afc_operation op)</span></span>;</div><div class="line"> <span class="function">CFTypeID <span class="title">AFCOperationGetTypeID</span><span class="params">(afc_operation op)</span></span>;</div><div class="line"> <span class="keyword">afc_error_t</span> AFCOperationSetContext(afc_operation op, <span class="keyword">void</span> *ctx);</div><div class="line"> <span class="function"><span class="keyword">void</span> *<span class="title">AFCOperationGetContext</span><span class="params">(afc_operation op)</span></span>;</div><div class="line"> </div><div class="line"> <span class="comment">// each of these returns an op, with the appropriate request encoded. The value of ctx is</span></div><div class="line"> <span class="comment">// available via AFCOperationGetContext()</span></div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateGetConnectionInfo</span><span class="params">(CFAllocatorRef allocator, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateGetDeviceInfo</span><span class="params">(CFAllocatorRef allocator, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateGetFileHash</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateGetFileInfo</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateLinkPath</span><span class="params">(CFAllocatorRef allocator, <span class="keyword">uint32_t</span> mode, CFStringRef filename1, CFStringRef filename2, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateMakeDirectory</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateOpenFile</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateReadDirectory</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateRemovePath</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateRenamePath</span><span class="params">(CFAllocatorRef allocator, CFStringRef oldname, CFStringRef newname, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateSetConnectionOptions</span><span class="params">(CFAllocatorRef allocator, CFDictionaryRef dict, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> <span class="function">afc_operation <span class="title">AFCOperationCreateSetModTime</span><span class="params">(CFAllocatorRef allocator, CFStringRef filename, <span class="keyword">uint64_t</span> mtm, <span class="keyword">void</span> *ctx)</span></span>;</div><div class="line"> </div><div class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> __cplusplus</span></div><div class="line">}</div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div></pre></td></tr></table></figure></blockquote><p>看到这里你可能会想问返回值中 <strong><em>kAMDError</em></strong> 是什么? 别急,往下看<br>其实 <strong><em>kAMDError</em></strong> 其实只是一个枚举 :</p><blockquote><h5 id="kAMDError-Code"><a href="#kAMDError-Code" class="headerlink" title="kAMDError Code"></a><strong>kAMDError Code</strong></h5><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">typedef</span> <span class="keyword">enum</span> kAMDError {</div><div class="line"> </div><div class="line"> kAMDAlreadyArchivedError = <span class="number">-402653094</span>,</div><div class="line"> kAMDApplicationAlreadyInstalledError = <span class="number">-402653130</span>,</div><div class="line"> kAMDApplicationMoveFailedError,</div><div class="line"> kAMDApplicationSandboxFailedError = <span class="number">-402653127</span>,</div><div class="line"> kAMDApplicationSINFCaptureFailedError = <span class="number">-402653128</span>,</div><div class="line"> kAMDApplicationVerificationFailedError = <span class="number">-402653126</span>,</div><div class="line"> kAMDArchiveDestructionFailedError,</div><div class="line"> kAMDBadHeaderError = <span class="number">-402653182</span>,</div><div class="line"> kAMDBundleVerificationFailedError = <span class="number">-402653124</span>,</div><div class="line"> kAMDBusyError = <span class="number">-402653167</span>,</div><div class="line"> kAMDCarrierBundleCopyFailedError = <span class="number">-402653123</span>,</div><div class="line"> kAMDCarrierBundleDirectoryCreationFailedError,</div><div class="line"> kAMDCarrierBundleMissingSupportedSIMsError,</div><div class="line"> kAMDCheckinTimeoutError = <span class="number">-402653148</span>,</div><div class="line"> kAMDCommCenterNotificationFailedError = <span class="number">-402653120</span>,</div><div class="line"> kAMDContainerCreationFailedError,</div><div class="line"> kAMDContainerP0wnFailedError,</div><div class="line"> kAMDContainerRemovalFailedError,</div><div class="line"> kAMDCryptoError = <span class="number">-402653166</span>,</div><div class="line"> kAMDDigestFailedError = <span class="number">-402653135</span>,</div><div class="line"> kAMDEmbeddedProfileInstallFailedError = <span class="number">-402653116</span>,</div><div class="line"> kAMDEOFError = <span class="number">-402653170</span>,</div><div class="line"> kAMDErrorError = <span class="number">-402653115</span>,</div><div class="line"> kAMDExecutableTwiddleFailedError,</div><div class="line"> kAMDExistenceCheckFailedError,</div><div class="line"> kAMDFileExistsError = <span class="number">-402653168</span>,</div><div class="line"> kAMDGetProhibitedError = <span class="number">-402653162</span>,</div><div class="line"> kAMDImmutableValueError = <span class="number">-402653159</span>,</div><div class="line"> kAMDInstallMapUpdateFailedError = <span class="number">-402653112</span>,</div><div class="line"> kAMDInvalidActivationRecordError = <span class="number">-402653146</span>,</div><div class="line"> kAMDInvalidArgumentError = <span class="number">-402653177</span>,</div><div class="line"> kAMDInvalidCheckinError = <span class="number">-402653149</span>,</div><div class="line"> kAMDInvalidDiskImageError = <span class="number">-402653133</span>,</div><div class="line"> kAMDInvalidHostIDError = <span class="number">-402653156</span>,</div><div class="line"> kAMDInvalidResponseError = <span class="number">-402653165</span>,</div><div class="line"> kAMDInvalidServiceError = <span class="number">-402653150</span>,</div><div class="line"> kAMDInvalidSessionIDError = <span class="number">-402653152</span>,</div><div class="line"> kAMDIsDirectoryError = <span class="number">-402653175</span>,</div><div class="line"> kAMDiTunesArtworkCaptureFailedError = <span class="number">-402653096</span>,</div><div class="line"> kAMDiTunesMetadataCaptureFailedError,</div><div class="line"> kAMDManifestCaptureFailedError = <span class="number">-402653111</span>,</div><div class="line"> kAMDMapGenerationFailedError,</div><div class="line"> kAMDMissingActivationRecordError = <span class="number">-402653145</span>,</div><div class="line"> kAMDMissingBundleExecutableError = <span class="number">-402653109</span>,</div><div class="line"> kAMDMissingBundleIdentifierError,</div><div class="line"> kAMDMissingBundlePathError,</div><div class="line"> kAMDMissingContainerError,</div><div class="line"> kAMDMissingDigestError = <span class="number">-402653132</span>,</div><div class="line"> kAMDMissingHostIDError = <span class="number">-402653157</span>,</div><div class="line"> kAMDMissingImageTypeError = <span class="number">-402653136</span>,</div><div class="line"> kAMDMissingKeyError = <span class="number">-402653164</span>,</div><div class="line"> kAMDMissingOptionsError = <span class="number">-402653137</span>,</div><div class="line"> kAMDMissingPairRecordError = <span class="number">-402653147</span>,</div><div class="line"> kAMDMissingServiceError = <span class="number">-402653151</span>,</div><div class="line"> kAMDMissingSessionIDError = <span class="number">-402653153</span>,</div><div class="line"> kAMDMissingValueError = <span class="number">-402653163</span>,</div><div class="line"> kAMDMuxError = <span class="number">-402653131</span>,</div><div class="line"> kAMDNoResourcesError = <span class="number">-402653181</span>,</div><div class="line"> kAMDNotConnectedError = <span class="number">-402653173</span>,</div><div class="line"> kAMDNotFoundError = <span class="number">-402653176</span>,</div><div class="line"> kAMDNotificationFailedError = <span class="number">-402653105</span>,</div><div class="line"> kAMDOverrunError = <span class="number">-402653171</span>,</div><div class="line"> kAMDPackageExtractionFailedError = <span class="number">-402653104</span>,</div><div class="line"> kAMDPackageInspectionFailedError,</div><div class="line"> kAMDPackageMoveFailedError,</div><div class="line"> kAMDPasswordProtectedError = <span class="number">-402653158</span>,</div><div class="line"> kAMDPathConversionFailedError = <span class="number">-402653101</span>,</div><div class="line"> kAMDPermissionError = <span class="number">-402653174</span>,</div><div class="line"> kAMDProvisioningProfileNotValid = <span class="number">-402653140</span>,</div><div class="line"> kAMDReadError = <span class="number">-402653180</span>,</div><div class="line"> kAMDReceiveMessageError = <span class="number">-402653138</span>,</div><div class="line"> kAMDRemoveProhibitedError = <span class="number">-402653160</span>,</div><div class="line"> kAMDRestoreContainerFailedError = <span class="number">-402653100</span>,</div><div class="line"> kAMDSeatbeltProfileRemovalFailedError,</div><div class="line"> kAMDSendMessageError = <span class="number">-402653139</span>,</div><div class="line"> kAMDSessionActiveError = <span class="number">-402653155</span>,</div><div class="line"> kAMDSessionInactiveError,</div><div class="line"> kAMDSetProhibitedError = <span class="number">-402653161</span>,</div><div class="line"> kAMDStageCreationFailedError = <span class="number">-402653098</span>,</div><div class="line"> kAMDStartServiceError = <span class="number">-402653134</span>,</div><div class="line"> kAMDSuccess = <span class="number">0</span>,</div><div class="line"> kAMDSUFirmwareError = <span class="number">-402653141</span>,</div><div class="line"> kAMDSUPatchError = <span class="number">-402653142</span>,</div><div class="line"> kAMDSUVerificationError = <span class="number">-402653143</span>,</div><div class="line"> kAMDSymlinkFailedError = <span class="number">-402653097</span>,</div><div class="line"> kAMDTimeOutError = <span class="number">-402653172</span>,</div><div class="line"> kAMDUndefinedError = <span class="number">-402653183</span>,</div><div class="line"> kAMDUnknownPacketError = <span class="number">-402653178</span>,</div><div class="line"> kAMDUnsupportedError = <span class="number">-402653169</span>,</div><div class="line"> kAMDWriteError = <span class="number">-402653179</span>,</div><div class="line"> kAMDWrongDroidError = <span class="number">-402653144</span>,</div><div class="line"> kAMDTrustComputerError = <span class="number">-402653034</span></div><div class="line">} kAMDError;</div></pre></td></tr></table></figure></blockquote><p><strong>嗯 先说到这里吧。 博主大大懒惰症又犯了…XD</strong></p>]]></content>
<summary type="html">
<blockquote>
<p><strong>Hi, 太久没有写文章了,准备勤快的更新了。文章以后将开始用简体中文开始写了, 可能有词还是繁体直译,看不懂莫怪,哈哈(跑题了) 言归正传</strong></p>
</blockquote>
<hr>
<h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a><strong>前言</strong></h4><p>如果你厌恶iTunes带来的繁琐,却想要通过macOS来连接你的iPhone, 并和iTunes一样管理你的手机,甚至简化一些步骤,你可能需要看看这篇文章了。想要和iTunes一样连接管理iPhone装置,我们需要了解到他的private api,这可能需要你懂得反组译,我这里并不准备说明怎么反组译,你可能需要自行google it。<br><strong>so sad : (</strong><br>
</summary>
<category term="Mac 开发" scheme="http://blog.martinlau.org/categories/Mac-%E5%BC%80%E5%8F%91/"/>
<category term="Mac, iOS助手开发" scheme="http://blog.martinlau.org/tags/Mac-iOS%E5%8A%A9%E6%89%8B%E5%BC%80%E5%8F%91/"/>
</entry>
<entry>
<title>iOS助手开发2-设备连接</title>
<link href="http://blog.martinlau.org/2017/09/iOS%E5%8A%A9%E6%89%8B%E5%BC%80%E5%8F%912-%E8%AE%BE%E5%A4%87%E8%BF%9E%E6%8E%A5/"/>
<id>http://blog.martinlau.org/2017/09/iOS助手开发2-设备连接/</id>
<published>2017-09-19T06:35:08.000Z</published>
<updated>2017-09-19T06:35:08.000Z</updated>
<content type="html"><![CDATA[<h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a><strong>前言</strong></h4><p>在上一篇文章讲到MobileDevice.framework中的私有API, 那么这篇文章我们将讲讲怎么监听装置连线和撷取连线后装置的资料。</p><hr><h4 id="Demo"><a href="#Demo" class="headerlink" title="Demo"></a>Demo</h4><p>文章中讲到的内容可参照demo来学习<a id="more"></a><br>Demo请通过github载点进行<em><a href="https://github.com/MartinLau7/MSMobileDeviceDemo" target="_blank" rel="external">下载</a></em></p><hr><h4 id="Exercises"><a href="#Exercises" class="headerlink" title="Exercises"></a>Exercises</h4><h5 id="环境需要"><a href="#环境需要" class="headerlink" title="环境需要"></a>环境需要</h5><blockquote><ul><li><strong>iTunes12+</strong> </li><li><strong>MobileDevice.framework</strong></li></ul></blockquote><h5 id="专案准备"><a href="#专案准备" class="headerlink" title="专案准备"></a>专案准备</h5><blockquote><h4 id="setp-1"><a href="#setp-1" class="headerlink" title="setp 1"></a>setp 1</h4><ol><li>首先我们需要new a project on xcode, project name随你所好吧</li><li>建立 <a href="../iOS助手开发1-基础/">iOS助手开发1-基础</a>中的private api headers<br><img src="[email protected]" width="400" height="100"></li><li>引入private framework (<a href="../iOS助手开发1-基础/">参考iOS助手开发1-基础</a>)<br><img src="[email protected]" width="400" height="100"><br><img src="[email protected]" width="400" height="100"></li></ol><h4 id="setp-2"><a href="#setp-2" class="headerlink" title="setp 2"></a>setp 2</h4></blockquote><h5 id="1-xo-添加MSMobileDeviceManager-cocoa-object"><a href="#1-xo-添加MSMobileDeviceManager-cocoa-object" class="headerlink" title="1.xo 添加MSMobileDeviceManager cocoa object"></a>1.xo 添加<strong>MSMobileDeviceManager</strong> cocoa object</h5><blockquote><p><strong>MSMobileDeviceManager.h</strong></p></blockquote><figure class="highlight mm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// MSMobileDeviceManager.h</span></div><div class="line"><span class="comment">// MSMobileDeviceDemo</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Created by MartinLau on 2017/5/18.</span></div><div class="line"><span class="comment">// Copyright © 2017年 Martin Studio. All rights reserved.</span></div><div class="line"><span class="comment">//</span></div><div class="line"></div><div class="line"><span class="meta">#import <span class="meta-string"><Foundation/Foundation.h></span></span></div><div class="line"></div><div class="line"><span class="keyword">extern</span> <span class="built_in">NSString</span> * <span class="keyword">const</span> kMSDeviceConnectedNotification; <span class="comment">//装置连接</span></div><div class="line"><span class="keyword">extern</span> <span class="built_in">NSString</span> * <span class="keyword">const</span> kMSDeviceDisConnecteNotification; <span class="comment">//装置断开</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">MSMobileDeviceManager</span> : <span class="title">NSObject</span></span></div><div class="line"></div><div class="line">+ (MSMobileDeviceManager *)sharedManager;</div><div class="line"></div><div class="line"><span class="keyword">@end</span></div></pre></td></tr></table></figure><blockquote><p> <strong>MSMobileDeviceManager.m</strong></p></blockquote><figure class="highlight mm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// MSMobileDeviceManager.m</span></div><div class="line"><span class="comment">// MSMobileDeviceDemo</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Created by MartinLau on 2017/5/18.</span></div><div class="line"><span class="comment">// Copyright © 2017年 Martin Studio. All rights reserved.</span></div><div class="line"><span class="comment">//</span></div><div class="line"></div><div class="line"><span class="meta">#import <span class="meta-string">"MSMobileDevice.h"</span></span></div><div class="line"><span class="meta">#import <span class="meta-string">"MSMobileDeviceManager.h"</span></span></div><div class="line"></div><div class="line"></div><div class="line"><span class="built_in">NSString</span> * <span class="keyword">const</span> kMSDeviceConnectedNotification = <span class="string">@"MSDeviceConnectedNotification"</span>;</div><div class="line"><span class="built_in">NSString</span> * <span class="keyword">const</span> kMSDeviceDisConnecteNotification = <span class="string">@"MSDeviceDisConnecteNotification"</span>;</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">MSMobileDeviceManager</span> () </span>{</div><div class="line"><span class="keyword">@private</span></div><div class="line"> <span class="keyword">id</span> _listener;</div><div class="line"> <span class="built_in">BOOL</span> _subscribed;</div><div class="line"> am_device_notification _notification;</div><div class="line"> <span class="built_in">BOOL</span> _waitingInRunLoop;</div><div class="line"> </div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">@end</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">MSMobileDeviceManager</span></span></div><div class="line"></div><div class="line">+ (MSMobileDeviceManager *)sharedManager</div><div class="line">{</div><div class="line"> <span class="keyword">static</span> <span class="built_in">dispatch_once_t</span> pred = <span class="number">0</span>;</div><div class="line"> __<span class="keyword">strong</span> <span class="keyword">static</span> <span class="keyword">id</span> _sharedObject = <span class="literal">nil</span>;</div><div class="line"> <span class="built_in">dispatch_once</span>(&pred, ^{</div><div class="line"> _sharedObject = [[<span class="keyword">self</span> alloc] init]; <span class="comment">// or some other init method</span></div><div class="line"> });</div><div class="line"> <span class="keyword">return</span> _sharedObject;</div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">instancetype</span>)init</div><div class="line">{</div><div class="line"> <span class="keyword">self</span> = [<span class="keyword">super</span> init];</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">self</span>) {</div><div class="line"> </div><div class="line"> [<span class="keyword">self</span> initEnvironment];</div><div class="line"> [<span class="keyword">self</span> startListeningDevicesConnection];</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="keyword">self</span>;</div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)dealloc</div><div class="line">{</div><div class="line"> [[<span class="built_in">NSNotificationCenter</span> defaultCenter] removeObserver:<span class="keyword">self</span>];</div><div class="line"> </div><div class="line"> [<span class="keyword">self</span> stopListeningDeviceConnection];</div><div class="line"> <span class="keyword">if</span> (_waitingInRunLoop) <span class="built_in">CFRunLoopStop</span>(<span class="built_in">CFRunLoopGetCurrent</span>());</div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)initEnvironment {</div><div class="line"> </div><div class="line"> _subscribed = <span class="literal">NO</span>;<span class="comment">// we are not subscribed yet</span></div><div class="line"> _waitingInRunLoop = <span class="literal">NO</span>;<span class="comment">// we are not currently waiting in a runloop</span></div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// this is (indirectly) called back by AMDeviceNotificationSubscribe()</span></div><div class="line"><span class="comment">// whenever something interesting happens</span></div><div class="line">- (<span class="keyword">void</span>)Notify:(<span class="keyword">struct</span> am_device_notification_callback_info*)info</div><div class="line">{</div><div class="line"> <span class="comment">//过滤WIFI设备</span></div><div class="line"> <span class="keyword">if</span> (AMDeviceGetInterfaceType(info->dev) == <span class="number">2</span>) {</div><div class="line"> </div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">switch</span> (info->msg) {</div><div class="line"> <span class="keyword">case</span> ADNCI_MSG_UNSUBSCRIBED:</div><div class="line"> <span class="built_in">NSLog</span>(<span class="string">@"Unkown error of USB device connected"</span>);</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> </div><div class="line"> <span class="keyword">case</span> ADNCI_MSG_CONNECTED:</div><div class="line"> {</div><div class="line"> </div><div class="line"> am_device device = info->dev;</div><div class="line"> uint32_t error = AMDeviceConnect(device);</div><div class="line"> <span class="keyword">if</span> (error == <span class="number">0</span>) {</div><div class="line"> </div><div class="line"> error = AMDeviceStartSession(device);</div><div class="line"> </div><div class="line"> <span class="keyword">if</span> (error == <span class="number">0</span>) {</div><div class="line"> </div><div class="line"> <span class="comment">//get device info</span></div><div class="line"> <span class="built_in">CFTypeRef</span> typeRef = AMDeviceCopyValue(device, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</div><div class="line"> </div><div class="line"> <span class="keyword">id</span> result;</div><div class="line"> <span class="keyword">if</span> (typeRef) {</div><div class="line"> </div><div class="line"> result = (__bridge <span class="keyword">id</span>)(typeRef);</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">if</span> (typeRef != <span class="literal">NULL</span>) {</div><div class="line"> </div><div class="line"> <span class="built_in">CFRelease</span>(typeRef);</div><div class="line"> }</div><div class="line"> </div><div class="line"> [<span class="keyword">self</span> deviceConnectionNotificationHandler:@{<span class="string">@"MessageType"</span>:<span class="string">@"Attached"</span>, <span class="string">@"Device"</span>:result}];</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> ADNCI_MSG_DISCONNECTED:</div><div class="line"> {</div><div class="line"> <span class="built_in">NSString</span> *uniqueId = (__bridge <span class="built_in">NSString</span> *)(AMDeviceCopyDeviceIdentifier(info->dev));</div><div class="line"> </div><div class="line"> [<span class="keyword">self</span> deviceConnectionNotificationHandler:@{<span class="string">@"MessageType"</span>:<span class="string">@"Detached"</span>, <span class="string">@"UniqueId"</span>:uniqueId}];</div><div class="line"> }</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> </div><div class="line"> <span class="keyword">default</span>:</div><div class="line"><span class="meta">#if DEBUG</span></div><div class="line"> <span class="built_in">NSLog</span>(<span class="string">@"Ignoring unknown message: %d"</span>,info->msg);</div><div class="line"><span class="meta">#endif</span></div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">// if he's waiting for us to do something, break him</span></div><div class="line"> <span class="keyword">if</span> (_waitingInRunLoop) <span class="built_in">CFRunLoopStop</span>(<span class="built_in">CFRunLoopGetCurrent</span>());</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">void</span> notify_callback(<span class="keyword">struct</span> am_device_notification_callback_info *info, <span class="keyword">void</span>* arg)</div><div class="line">{</div><div class="line"> <span class="keyword">@autoreleasepool</span> {</div><div class="line"> </div><div class="line"> MSMobileDeviceManager *objSelf = (__bridge MSMobileDeviceManager *)(arg);</div><div class="line"> [objSelf Notify:info];</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)startListeningDevicesConnection {</div><div class="line"> </div><div class="line"> <span class="keyword">if</span> (!_subscribed) {</div><div class="line"> </div><div class="line"> <span class="keyword">void</span> *objSelf = (__bridge_retained <span class="keyword">void</span> *)(<span class="keyword">self</span>);</div><div class="line"> </div><div class="line"> <span class="comment">// try to subscribe for notifications - pass self as the callback_data</span></div><div class="line"> <span class="keyword">int</span> ret = AMDeviceNotificationSubscribe(notify_callback, <span class="number">0</span>, <span class="number">0</span>, objSelf, &_notification);</div><div class="line"> <span class="keyword">if</span> (ret == <span class="number">0</span>) {</div><div class="line"> _subscribed = <span class="literal">YES</span>;</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="comment">// we should throw or something in here...</span></div><div class="line"> <span class="built_in">NSLog</span>(<span class="string">@"AMDeviceNotificationSubscribe failed: %d"</span>, ret);</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)stopListeningDeviceConnection {</div><div class="line"> </div><div class="line"> <span class="keyword">if</span> (_subscribed && _notification) {</div><div class="line"> </div><div class="line"> uint32_t error = AMDeviceNotificationUnsubscribe(_notification);</div><div class="line"> <span class="keyword">if</span> (error == <span class="number">0</span>) {</div><div class="line"> </div><div class="line"> _subscribed = <span class="literal">NO</span>;</div><div class="line"> _notification = <span class="literal">NULL</span>;</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> </div><div class="line"> <span class="comment">// we should throw or something in here...</span></div><div class="line"> <span class="built_in">NSLog</span>(<span class="string">@"AMDeviceNotificationUnsubscribe failed: %d"</span>, error);</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)deviceConnectionNotificationHandler:(<span class="built_in">NSDictionary</span> *)packet {</div><div class="line"> </div><div class="line"> <span class="built_in">NSString</span> *notificationName;</div><div class="line"> <span class="built_in">NSString</span> *messageType = [packet objectForKey:<span class="string">@"MessageType"</span>];</div><div class="line"> </div><div class="line"> <span class="keyword">if</span> ([messageType isEqualToString:<span class="string">@"Attached"</span>]) {</div><div class="line"> </div><div class="line"> notificationName = kMSDeviceConnectedNotification;</div><div class="line"> }</div><div class="line"> <span class="keyword">if</span> ([messageType isEqualToString:<span class="string">@"Detached"</span>]) {</div><div class="line"> </div><div class="line"> notificationName = kMSDeviceDisConnecteNotification;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">if</span> (notificationName) {</div><div class="line"> </div><div class="line"> [[<span class="built_in">NSNotificationCenter</span> defaultCenter] postNotificationName:notificationName object:<span class="keyword">self</span> userInfo:packet];</div><div class="line"> }</div><div class="line">}</div><div class="line"><span class="keyword">@end</span></div></pre></td></tr></table></figure><blockquote><p><strong>代码说明</strong> </p></blockquote><ul><li><strong>AMDeviceNotificationSubscribe</strong> <p>用于注册设备连接断开事件委托</p></li><li><strong>AMDeviceNotificationUnsubscribe</strong> <p>用于销毁设备连接断开事件委托</p></li><li><strong>AMDeviceGetInterfaceType</strong> <p>该方法用于判断设备处于WIFI连接还是USB连接</p></li></ul><h5 id="2-在AppDelegate-m-监控连接设备"><a href="#2-在AppDelegate-m-监控连接设备" class="headerlink" title="2.在AppDelegate.m 监控连接设备"></a>2.在AppDelegate.m 监控连接设备</h5><blockquote><p><strong>AppDelegate.m</strong></p></blockquote><figure class="highlight mm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// AppDelegate.m</span></div><div class="line"><span class="comment">// MSMobileDeviceDemo</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Created by MartinLau on 2017/3/19.</span></div><div class="line"><span class="comment">// Copyright © 2017年 Martin Studio. All rights reserved.</span></div><div class="line"><span class="comment">//</span></div><div class="line"></div><div class="line"><span class="meta">#import <span class="meta-string">"AppDelegate.h"</span></span></div><div class="line"><span class="meta">#import <span class="meta-string">"MSMobileDeviceManager.h"</span></span></div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">AppDelegate</span> ()</span></div><div class="line"></div><div class="line"><span class="keyword">@property</span> (<span class="keyword">weak</span>) <span class="keyword">IBOutlet</span> <span class="built_in">NSWindow</span> *window;</div><div class="line"><span class="keyword">@end</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">AppDelegate</span></span></div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)applicationDidFinishLaunching:(<span class="built_in">NSNotification</span> *)aNotification {</div><div class="line"> <span class="comment">// Insert code here to initialize your application</span></div><div class="line"> </div><div class="line"> [[<span class="built_in">NSNotificationCenter</span> defaultCenter] addObserverForName:kMSDeviceConnectedNotification object:[MSMobileDeviceManager sharedManager] queue:<span class="literal">nil</span> usingBlock:^(<span class="built_in">NSNotification</span> * _Nonnull note) {</div><div class="line"> </div><div class="line"> <span class="built_in">NSLog</span>(<span class="string">@"%@"</span>, note.userInfo);</div><div class="line"> }];</div><div class="line"> </div><div class="line">}</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)applicationWillTerminate:(<span class="built_in">NSNotification</span> *)aNotification {</div><div class="line"> <span class="comment">// Insert code here to tear down your application</span></div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">@end</span></div></pre></td></tr></table></figure><p>好了 开始你们的玩具时刻了。XD~</p>]]></content>
<summary type="html">
<h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a><strong>前言</strong></h4><p>在上一篇文章讲到MobileDevice.framework中的私有API, 那么这篇文章我们将讲讲怎么监听装置连线和撷取连线后装置的资料。</p>
<hr>
<h4 id="Demo"><a href="#Demo" class="headerlink" title="Demo"></a>Demo</h4><p>文章中讲到的内容可参照demo来学习
</summary>
<category term="Mac 开发" scheme="http://blog.martinlau.org/categories/Mac-%E5%BC%80%E5%8F%91/"/>
<category term="Mac, iOS助手开发" scheme="http://blog.martinlau.org/tags/Mac-iOS%E5%8A%A9%E6%89%8B%E5%BC%80%E5%8F%91/"/>
</entry>
<entry>
<title>importPKCS12</title>
<link href="http://blog.martinlau.org/2016/06/importPKCS12/"/>
<id>http://blog.martinlau.org/2016/06/importPKCS12/</id>
<published>2016-06-05T19:03:00.000Z</published>
<updated>2016-06-05T19:03:00.000Z</updated>
<content type="html"><![CDATA[<p>群友催我更新博客了, 正好最近手上專案有個證書匯入系統case, 就拿來貢獻了。 嗯~博主是懶人, 總喜歡做工具伺候我的懶!!!!</p><p>做iOS程式開發, 應該都有體驗,root cer總是一個team唯一並分發的, 那麼如果做助手類就需要更多的開發帳號了,要匯入更多的root cer了,所以博主便寫了一個批量匯入root cer(PKCS12-*.12)</p><a id="more"></a><p>主要代碼如下 :<br><figure class="highlight mm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">- (<span class="built_in">BOOL</span>)importPKCS12WithPKC12:(<span class="built_in">NSString</span> *)pkcs12File {</div><div class="line"></div><div class="line"> <span class="built_in">NSString</span> *p12FilePath = pkcs12File;</div><div class="line"></div><div class="line"> OSStatus status = errSecSuccess;</div><div class="line"> <span class="built_in">NSData</span> *p12Data = [<span class="built_in">NSData</span> dataWithContentsOfFile:p12FilePath];</div><div class="line"> <span class="built_in">CFDataRef</span> p12DataRef = (__bridge <span class="built_in">CFDataRef</span>)p12Data;</div><div class="line"> <span class="built_in">NSMutableDictionary</span> *options = [<span class="built_in">NSMutableDictionary</span> dictionary];</div><div class="line"> <span class="built_in">NSString</span> *key = (__bridge <span class="built_in">NSString</span> *)kSecImportExportPassphrase;</div><div class="line"> options[key] = <span class="string">@"123456"</span>;</div><div class="line"></div><div class="line"> <span class="built_in">CFArrayRef</span> items = <span class="built_in">CFArrayCreate</span>(<span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="literal">NULL</span>);</div><div class="line"> status = SecPKCS12Import(p12DataRef, (__bridge <span class="built_in">CFDictionaryRef</span>)options, &items);</div><div class="line"></div><div class="line"> <span class="keyword">if</span> (status != errSecSuccess) {</div><div class="line"> <span class="built_in">NSLog</span>(<span class="string">@"P12证书导入失败,错误号:%d"</span>, (<span class="keyword">int</span>)status);</div><div class="line"> <span class="keyword">return</span> <span class="literal">NO</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">return</span> <span class="literal">YES</span>;</div><div class="line">}</div></pre></td></tr></table></figure></p><p>其中key是p12的密钥.</p>]]></content>
<summary type="html">
<p>群友催我更新博客了, 正好最近手上專案有個證書匯入系統case, 就拿來貢獻了。 嗯~博主是懶人, 總喜歡做工具伺候我的懶!!!!</p>
<p>做iOS程式開發, 應該都有體驗,root cer總是一個team唯一並分發的, 那麼如果做助手類就需要更多的開發帳號了,要匯入更多的root cer了,所以博主便寫了一個批量匯入root cer(PKCS12-*.12)</p>
</summary>
<category term="Mac 开发" scheme="http://blog.martinlau.org/categories/Mac-%E5%BC%80%E5%8F%91/"/>
<category term="PKCS12, Mac" scheme="http://blog.martinlau.org/tags/PKCS12-Mac/"/>
</entry>
<entry>
<title>萬事開頭難,第一篇文章出生</title>
<link href="http://blog.martinlau.org/2016/04/FirstArticle/"/>
<id>http://blog.martinlau.org/2016/04/FirstArticle/</id>
<published>2016-04-01T19:03:00.000Z</published>
<updated>2016-04-01T19:03:00.000Z</updated>
<content type="html"><![CDATA[<p>2年了,嘴上總是說要開個點落格寫寫網誌, 紀錄一點一滴積累的技術。但總是說完就算數,欸!我有健忘症好嗎?</p><a id="more"></a><p>不過一切總會好的,不是嗎?現在我還是開了這個點落格來開始記錄技術的小故事啊,XD<br>嗯~放心啦,我一般只會記得時候來記錄CoCoa and 。NET的故事,哈!?不是我的故事啦。<br>fine,我超懶又不爽寫太多文字,就這樣好了</p><p>[對了,要是想要交換連接可以 send mail 聯絡我!]</p>]]></content>
<summary type="html">
<p>2年了,嘴上總是說要開個點落格寫寫網誌, 紀錄一點一滴積累的技術。但總是說完就算數,欸!我有健忘症好嗎?</p>
</summary>
<category term="隨手記" scheme="http://blog.martinlau.org/categories/%E9%9A%A8%E6%89%8B%E8%A8%98/"/>
<category term="隨手記" scheme="http://blog.martinlau.org/tags/%E9%9A%A8%E6%89%8B%E8%A8%98/"/>
</entry>
</feed>