lipo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**************************************************************************/
  2. /* lipo.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "lipo.h"
  31. bool LipO::is_lipo(const String &p_path) {
  32. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::READ);
  33. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  34. uint32_t magic = fb->get_32();
  35. return (magic == 0xbebafeca || magic == 0xcafebabe || magic == 0xbfbafeca || magic == 0xcafebabf);
  36. }
  37. bool LipO::create_file(const String &p_output_path, const Vector<String> &p_files) {
  38. close();
  39. fa = FileAccess::open(p_output_path, FileAccess::WRITE);
  40. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_output_path));
  41. uint64_t max_size = 0;
  42. for (int i = 0; i < p_files.size(); i++) {
  43. {
  44. MachO mh;
  45. if (!mh.open_file(p_files[i])) {
  46. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid MachO file: \"%s\".", p_files[i]));
  47. }
  48. FatArch arch;
  49. arch.cputype = mh.get_cputype();
  50. arch.cpusubtype = mh.get_cpusubtype();
  51. arch.offset = 0;
  52. arch.size = mh.get_size();
  53. arch.align = mh.get_align();
  54. max_size += arch.size;
  55. archs.push_back(arch);
  56. }
  57. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  58. if (fb.is_null()) {
  59. close();
  60. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  61. }
  62. }
  63. // Write header.
  64. bool is_64 = (max_size >= std::numeric_limits<uint32_t>::max());
  65. if (is_64) {
  66. fa->store_32(0xbfbafeca);
  67. } else {
  68. fa->store_32(0xbebafeca);
  69. }
  70. fa->store_32(BSWAP32(archs.size()));
  71. uint64_t offset = archs.size() * (is_64 ? 32 : 20) + 8;
  72. for (int i = 0; i < archs.size(); i++) {
  73. archs.write[i].offset = offset + PAD(offset, uint64_t(1) << archs[i].align);
  74. if (is_64) {
  75. fa->store_32(BSWAP32(archs[i].cputype));
  76. fa->store_32(BSWAP32(archs[i].cpusubtype));
  77. fa->store_64(BSWAP64(archs[i].offset));
  78. fa->store_64(BSWAP64(archs[i].size));
  79. fa->store_32(BSWAP32(archs[i].align));
  80. fa->store_32(0);
  81. } else {
  82. fa->store_32(BSWAP32(archs[i].cputype));
  83. fa->store_32(BSWAP32(archs[i].cpusubtype));
  84. fa->store_32(BSWAP32(archs[i].offset));
  85. fa->store_32(BSWAP32(archs[i].size));
  86. fa->store_32(BSWAP32(archs[i].align));
  87. }
  88. offset = archs[i].offset + archs[i].size;
  89. }
  90. // Write files and padding.
  91. for (int i = 0; i < archs.size(); i++) {
  92. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  93. if (fb.is_null()) {
  94. close();
  95. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  96. }
  97. uint64_t cur = fa->get_position();
  98. for (uint64_t j = cur; j < archs[i].offset; j++) {
  99. fa->store_8(0);
  100. }
  101. int pages = archs[i].size / 4096;
  102. int remain = archs[i].size % 4096;
  103. unsigned char step[4096];
  104. for (int j = 0; j < pages; j++) {
  105. uint64_t br = fb->get_buffer(step, 4096);
  106. if (br > 0) {
  107. fa->store_buffer(step, br);
  108. }
  109. }
  110. uint64_t br = fb->get_buffer(step, remain);
  111. if (br > 0) {
  112. fa->store_buffer(step, br);
  113. }
  114. }
  115. return true;
  116. }
  117. bool LipO::create_file(const String &p_output_path, const Vector<String> &p_files, const Vector<Vector2i> &p_cputypes) {
  118. close();
  119. fa = FileAccess::open(p_output_path, FileAccess::WRITE);
  120. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_output_path));
  121. ERR_FAIL_COND_V(p_files.size() != p_cputypes.size(), false);
  122. uint64_t max_size = 0;
  123. for (int i = 0; i < p_files.size(); i++) {
  124. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  125. if (fb.is_null()) {
  126. close();
  127. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  128. }
  129. {
  130. FatArch arch;
  131. MachO mh;
  132. if (MachO::is_macho(p_files[i]) && mh.open_file(p_files[i])) {
  133. arch.cputype = mh.get_cputype();
  134. arch.cpusubtype = mh.get_cpusubtype();
  135. arch.offset = 0;
  136. arch.size = mh.get_size();
  137. arch.align = mh.get_align();
  138. ERR_FAIL_V_MSG(arch.cputype != (uint32_t)p_cputypes[i].x || arch.cpusubtype != (uint32_t)p_cputypes[i].y, vformat("Mismatching MachO architecture: \"%s\".", p_files[i]));
  139. } else {
  140. arch.cputype = (uint32_t)p_cputypes[i].x;
  141. arch.cpusubtype = (uint32_t)p_cputypes[i].y;
  142. arch.offset = 0;
  143. arch.size = fb->get_length();
  144. arch.align = 0x03;
  145. }
  146. max_size += arch.size;
  147. archs.push_back(arch);
  148. }
  149. }
  150. // Write header.
  151. bool is_64 = (max_size >= std::numeric_limits<uint32_t>::max());
  152. if (is_64) {
  153. fa->store_32(0xbfbafeca);
  154. } else {
  155. fa->store_32(0xbebafeca);
  156. }
  157. fa->store_32(BSWAP32(archs.size()));
  158. uint64_t offset = archs.size() * (is_64 ? 32 : 20) + 8;
  159. for (int i = 0; i < archs.size(); i++) {
  160. archs.write[i].offset = offset + PAD(offset, uint64_t(1) << archs[i].align);
  161. if (is_64) {
  162. fa->store_32(BSWAP32(archs[i].cputype));
  163. fa->store_32(BSWAP32(archs[i].cpusubtype));
  164. fa->store_64(BSWAP64(archs[i].offset));
  165. fa->store_64(BSWAP64(archs[i].size));
  166. fa->store_32(BSWAP32(archs[i].align));
  167. fa->store_32(0);
  168. } else {
  169. fa->store_32(BSWAP32(archs[i].cputype));
  170. fa->store_32(BSWAP32(archs[i].cpusubtype));
  171. fa->store_32(BSWAP32(archs[i].offset));
  172. fa->store_32(BSWAP32(archs[i].size));
  173. fa->store_32(BSWAP32(archs[i].align));
  174. }
  175. offset = archs[i].offset + archs[i].size;
  176. }
  177. // Write files and padding.
  178. for (int i = 0; i < archs.size(); i++) {
  179. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  180. if (fb.is_null()) {
  181. close();
  182. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  183. }
  184. uint64_t cur = fa->get_position();
  185. for (uint64_t j = cur; j < archs[i].offset; j++) {
  186. fa->store_8(0);
  187. }
  188. int pages = archs[i].size / 4096;
  189. int remain = archs[i].size % 4096;
  190. unsigned char step[4096];
  191. for (int j = 0; j < pages; j++) {
  192. uint64_t br = fb->get_buffer(step, 4096);
  193. if (br > 0) {
  194. fa->store_buffer(step, br);
  195. }
  196. }
  197. uint64_t br = fb->get_buffer(step, remain);
  198. if (br > 0) {
  199. fa->store_buffer(step, br);
  200. }
  201. }
  202. return true;
  203. }
  204. bool LipO::open_file(const String &p_path) {
  205. close();
  206. fa = FileAccess::open(p_path, FileAccess::READ);
  207. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  208. uint32_t magic = fa->get_32();
  209. if (magic == 0xbebafeca) {
  210. // 32-bit fat binary, bswap.
  211. uint32_t nfat_arch = BSWAP32(fa->get_32());
  212. for (uint32_t i = 0; i < nfat_arch; i++) {
  213. FatArch arch;
  214. arch.cputype = BSWAP32(fa->get_32());
  215. arch.cpusubtype = BSWAP32(fa->get_32());
  216. arch.offset = BSWAP32(fa->get_32());
  217. arch.size = BSWAP32(fa->get_32());
  218. arch.align = BSWAP32(fa->get_32());
  219. archs.push_back(arch);
  220. }
  221. } else if (magic == 0xcafebabe) {
  222. // 32-bit fat binary.
  223. uint32_t nfat_arch = fa->get_32();
  224. for (uint32_t i = 0; i < nfat_arch; i++) {
  225. FatArch arch;
  226. arch.cputype = fa->get_32();
  227. arch.cpusubtype = fa->get_32();
  228. arch.offset = fa->get_32();
  229. arch.size = fa->get_32();
  230. arch.align = fa->get_32();
  231. archs.push_back(arch);
  232. }
  233. } else if (magic == 0xbfbafeca) {
  234. // 64-bit fat binary, bswap.
  235. uint32_t nfat_arch = BSWAP32(fa->get_32());
  236. for (uint32_t i = 0; i < nfat_arch; i++) {
  237. FatArch arch;
  238. arch.cputype = BSWAP32(fa->get_32());
  239. arch.cpusubtype = BSWAP32(fa->get_32());
  240. arch.offset = BSWAP64(fa->get_64());
  241. arch.size = BSWAP64(fa->get_64());
  242. arch.align = BSWAP32(fa->get_32());
  243. fa->get_32(); // Skip, reserved.
  244. archs.push_back(arch);
  245. }
  246. } else if (magic == 0xcafebabf) {
  247. // 64-bit fat binary.
  248. uint32_t nfat_arch = fa->get_32();
  249. for (uint32_t i = 0; i < nfat_arch; i++) {
  250. FatArch arch;
  251. arch.cputype = fa->get_32();
  252. arch.cpusubtype = fa->get_32();
  253. arch.offset = fa->get_64();
  254. arch.size = fa->get_64();
  255. arch.align = fa->get_32();
  256. fa->get_32(); // Skip, reserved.
  257. archs.push_back(arch);
  258. }
  259. } else {
  260. close();
  261. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid fat binary: \"%s\".", p_path));
  262. }
  263. return true;
  264. }
  265. int LipO::get_arch_count() const {
  266. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  267. return archs.size();
  268. }
  269. uint32_t LipO::get_arch_cputype(int p_index) const {
  270. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  271. ERR_FAIL_INDEX_V(p_index, archs.size(), 0);
  272. return archs[p_index].cputype;
  273. }
  274. uint32_t LipO::get_arch_cpusubtype(int p_index) const {
  275. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  276. ERR_FAIL_INDEX_V(p_index, archs.size(), 0);
  277. return archs[p_index].cpusubtype;
  278. }
  279. bool LipO::extract_arch(int p_index, const String &p_path) {
  280. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "LipO: File not opened.");
  281. ERR_FAIL_INDEX_V(p_index, archs.size(), false);
  282. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::WRITE);
  283. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  284. fa->seek(archs[p_index].offset);
  285. int pages = archs[p_index].size / 4096;
  286. int remain = archs[p_index].size % 4096;
  287. unsigned char step[4096];
  288. for (int i = 0; i < pages; i++) {
  289. uint64_t br = fa->get_buffer(step, 4096);
  290. if (br > 0) {
  291. fb->store_buffer(step, br);
  292. }
  293. }
  294. uint64_t br = fa->get_buffer(step, remain);
  295. if (br > 0) {
  296. fb->store_buffer(step, br);
  297. }
  298. return true;
  299. }
  300. void LipO::close() {
  301. archs.clear();
  302. }
  303. LipO::~LipO() {
  304. close();
  305. }