lipo.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*************************************************************************/
  2. /* lipo.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. MachO mh;
  44. if (!mh.open_file(p_files[i])) {
  45. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid MachO file: \"%s.\"", p_files[i]));
  46. }
  47. FatArch arch;
  48. arch.cputype = mh.get_cputype();
  49. arch.cpusubtype = mh.get_cpusubtype();
  50. arch.offset = 0;
  51. arch.size = mh.get_size();
  52. arch.align = mh.get_align();
  53. max_size += arch.size;
  54. archs.push_back(arch);
  55. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  56. if (fb.is_null()) {
  57. close();
  58. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s.\"", p_files[i]));
  59. }
  60. }
  61. // Write header.
  62. bool is_64 = (max_size >= std::numeric_limits<uint32_t>::max());
  63. if (is_64) {
  64. fa->store_32(0xbfbafeca);
  65. } else {
  66. fa->store_32(0xbebafeca);
  67. }
  68. fa->store_32(BSWAP32(archs.size()));
  69. uint64_t offset = archs.size() * (is_64 ? 32 : 20) + 8;
  70. for (int i = 0; i < archs.size(); i++) {
  71. archs.write[i].offset = offset + PAD(offset, uint64_t(1) << archs[i].align);
  72. if (is_64) {
  73. fa->store_32(BSWAP32(archs[i].cputype));
  74. fa->store_32(BSWAP32(archs[i].cpusubtype));
  75. fa->store_64(BSWAP64(archs[i].offset));
  76. fa->store_64(BSWAP64(archs[i].size));
  77. fa->store_32(BSWAP32(archs[i].align));
  78. fa->store_32(0);
  79. } else {
  80. fa->store_32(BSWAP32(archs[i].cputype));
  81. fa->store_32(BSWAP32(archs[i].cpusubtype));
  82. fa->store_32(BSWAP32(archs[i].offset));
  83. fa->store_32(BSWAP32(archs[i].size));
  84. fa->store_32(BSWAP32(archs[i].align));
  85. }
  86. offset = archs[i].offset + archs[i].size;
  87. }
  88. // Write files and padding.
  89. for (int i = 0; i < archs.size(); i++) {
  90. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  91. if (fb.is_null()) {
  92. close();
  93. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s.\"", p_files[i]));
  94. }
  95. uint64_t cur = fa->get_position();
  96. for (uint64_t j = cur; j < archs[i].offset; j++) {
  97. fa->store_8(0);
  98. }
  99. int pages = archs[i].size / 4096;
  100. int remain = archs[i].size % 4096;
  101. unsigned char step[4096];
  102. for (int j = 0; j < pages; j++) {
  103. uint64_t br = fb->get_buffer(step, 4096);
  104. if (br > 0) {
  105. fa->store_buffer(step, br);
  106. }
  107. }
  108. uint64_t br = fb->get_buffer(step, remain);
  109. if (br > 0) {
  110. fa->store_buffer(step, br);
  111. }
  112. }
  113. return true;
  114. }
  115. bool LipO::open_file(const String &p_path) {
  116. close();
  117. fa = FileAccess::open(p_path, FileAccess::READ);
  118. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  119. uint32_t magic = fa->get_32();
  120. if (magic == 0xbebafeca) {
  121. // 32-bit fat binary, bswap.
  122. uint32_t nfat_arch = BSWAP32(fa->get_32());
  123. for (uint32_t i = 0; i < nfat_arch; i++) {
  124. FatArch arch;
  125. arch.cputype = BSWAP32(fa->get_32());
  126. arch.cpusubtype = BSWAP32(fa->get_32());
  127. arch.offset = BSWAP32(fa->get_32());
  128. arch.size = BSWAP32(fa->get_32());
  129. arch.align = BSWAP32(fa->get_32());
  130. archs.push_back(arch);
  131. }
  132. } else if (magic == 0xcafebabe) {
  133. // 32-bit fat binary.
  134. uint32_t nfat_arch = fa->get_32();
  135. for (uint32_t i = 0; i < nfat_arch; i++) {
  136. FatArch arch;
  137. arch.cputype = fa->get_32();
  138. arch.cpusubtype = fa->get_32();
  139. arch.offset = fa->get_32();
  140. arch.size = fa->get_32();
  141. arch.align = fa->get_32();
  142. archs.push_back(arch);
  143. }
  144. } else if (magic == 0xbfbafeca) {
  145. // 64-bit fat binary, bswap.
  146. uint32_t nfat_arch = BSWAP32(fa->get_32());
  147. for (uint32_t i = 0; i < nfat_arch; i++) {
  148. FatArch arch;
  149. arch.cputype = BSWAP32(fa->get_32());
  150. arch.cpusubtype = BSWAP32(fa->get_32());
  151. arch.offset = BSWAP64(fa->get_64());
  152. arch.size = BSWAP64(fa->get_64());
  153. arch.align = BSWAP32(fa->get_32());
  154. fa->get_32(); // Skip, reserved.
  155. archs.push_back(arch);
  156. }
  157. } else if (magic == 0xcafebabf) {
  158. // 64-bit fat binary.
  159. uint32_t nfat_arch = fa->get_32();
  160. for (uint32_t i = 0; i < nfat_arch; i++) {
  161. FatArch arch;
  162. arch.cputype = fa->get_32();
  163. arch.cpusubtype = fa->get_32();
  164. arch.offset = fa->get_64();
  165. arch.size = fa->get_64();
  166. arch.align = fa->get_32();
  167. fa->get_32(); // Skip, reserved.
  168. archs.push_back(arch);
  169. }
  170. } else {
  171. close();
  172. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid fat binary: \"%s\".", p_path));
  173. }
  174. return true;
  175. }
  176. int LipO::get_arch_count() const {
  177. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  178. return archs.size();
  179. }
  180. bool LipO::extract_arch(int p_index, const String &p_path) {
  181. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "LipO: File not opened.");
  182. ERR_FAIL_INDEX_V(p_index, archs.size(), false);
  183. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::WRITE);
  184. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  185. fa->seek(archs[p_index].offset);
  186. int pages = archs[p_index].size / 4096;
  187. int remain = archs[p_index].size % 4096;
  188. unsigned char step[4096];
  189. for (int i = 0; i < pages; i++) {
  190. uint64_t br = fa->get_buffer(step, 4096);
  191. if (br > 0) {
  192. fb->store_buffer(step, br);
  193. }
  194. }
  195. uint64_t br = fa->get_buffer(step, remain);
  196. if (br > 0) {
  197. fb->store_buffer(step, br);
  198. }
  199. return true;
  200. }
  201. void LipO::close() {
  202. archs.clear();
  203. }
  204. LipO::~LipO() {
  205. close();
  206. }