lipo.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 PackedStringArray &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::open_file(const String &p_path) {
  118. close();
  119. fa = FileAccess::open(p_path, FileAccess::READ);
  120. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  121. uint32_t magic = fa->get_32();
  122. if (magic == 0xbebafeca) {
  123. // 32-bit fat binary, bswap.
  124. uint32_t nfat_arch = BSWAP32(fa->get_32());
  125. for (uint32_t i = 0; i < nfat_arch; i++) {
  126. FatArch arch;
  127. arch.cputype = BSWAP32(fa->get_32());
  128. arch.cpusubtype = BSWAP32(fa->get_32());
  129. arch.offset = BSWAP32(fa->get_32());
  130. arch.size = BSWAP32(fa->get_32());
  131. arch.align = BSWAP32(fa->get_32());
  132. archs.push_back(arch);
  133. }
  134. } else if (magic == 0xcafebabe) {
  135. // 32-bit fat binary.
  136. uint32_t nfat_arch = fa->get_32();
  137. for (uint32_t i = 0; i < nfat_arch; i++) {
  138. FatArch arch;
  139. arch.cputype = fa->get_32();
  140. arch.cpusubtype = fa->get_32();
  141. arch.offset = fa->get_32();
  142. arch.size = fa->get_32();
  143. arch.align = fa->get_32();
  144. archs.push_back(arch);
  145. }
  146. } else if (magic == 0xbfbafeca) {
  147. // 64-bit fat binary, bswap.
  148. uint32_t nfat_arch = BSWAP32(fa->get_32());
  149. for (uint32_t i = 0; i < nfat_arch; i++) {
  150. FatArch arch;
  151. arch.cputype = BSWAP32(fa->get_32());
  152. arch.cpusubtype = BSWAP32(fa->get_32());
  153. arch.offset = BSWAP64(fa->get_64());
  154. arch.size = BSWAP64(fa->get_64());
  155. arch.align = BSWAP32(fa->get_32());
  156. fa->get_32(); // Skip, reserved.
  157. archs.push_back(arch);
  158. }
  159. } else if (magic == 0xcafebabf) {
  160. // 64-bit fat binary.
  161. uint32_t nfat_arch = fa->get_32();
  162. for (uint32_t i = 0; i < nfat_arch; i++) {
  163. FatArch arch;
  164. arch.cputype = fa->get_32();
  165. arch.cpusubtype = fa->get_32();
  166. arch.offset = fa->get_64();
  167. arch.size = fa->get_64();
  168. arch.align = fa->get_32();
  169. fa->get_32(); // Skip, reserved.
  170. archs.push_back(arch);
  171. }
  172. } else {
  173. close();
  174. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid fat binary: \"%s\".", p_path));
  175. }
  176. return true;
  177. }
  178. int LipO::get_arch_count() const {
  179. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  180. return archs.size();
  181. }
  182. bool LipO::extract_arch(int p_index, const String &p_path) {
  183. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "LipO: File not opened.");
  184. ERR_FAIL_INDEX_V(p_index, archs.size(), false);
  185. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::WRITE);
  186. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  187. fa->seek(archs[p_index].offset);
  188. int pages = archs[p_index].size / 4096;
  189. int remain = archs[p_index].size % 4096;
  190. unsigned char step[4096];
  191. for (int i = 0; i < pages; i++) {
  192. uint64_t br = fa->get_buffer(step, 4096);
  193. if (br > 0) {
  194. fb->store_buffer(step, br);
  195. }
  196. }
  197. uint64_t br = fa->get_buffer(step, remain);
  198. if (br > 0) {
  199. fb->store_buffer(step, br);
  200. }
  201. return true;
  202. }
  203. void LipO::close() {
  204. archs.clear();
  205. }
  206. LipO::~LipO() {
  207. close();
  208. }