lipo.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "modules/modules_enabled.gen.h" // For regex.
  32. #ifdef MODULE_REGEX_ENABLED
  33. bool LipO::is_lipo(const String &p_path) {
  34. FileAccessRef fb = FileAccess::open(p_path, FileAccess::READ);
  35. ERR_FAIL_COND_V_MSG(!fb, false, vformat("LipO: Can't open file: \"%s\".", p_path));
  36. uint32_t magic = fb->get_32();
  37. return (magic == 0xbebafeca || magic == 0xcafebabe || magic == 0xbfbafeca || magic == 0xcafebabf);
  38. }
  39. bool LipO::create_file(const String &p_output_path, const PoolStringArray &p_files) {
  40. close();
  41. fa = FileAccess::open(p_output_path, FileAccess::WRITE);
  42. ERR_FAIL_COND_V_MSG(!fa, false, vformat("LipO: Can't open file: \"%s\".", p_output_path));
  43. uint64_t max_size = 0;
  44. for (int i = 0; i < p_files.size(); i++) {
  45. MachO mh;
  46. if (!mh.open_file(p_files[i])) {
  47. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  48. }
  49. FatArch arch;
  50. arch.cputype = mh.get_cputype();
  51. arch.cpusubtype = mh.get_cpusubtype();
  52. arch.offset = 0;
  53. arch.size = mh.get_size();
  54. arch.align = mh.get_align();
  55. max_size += arch.size;
  56. archs.push_back(arch);
  57. FileAccessRef fb = FileAccess::open(p_files[i], FileAccess::READ);
  58. if (!fb) {
  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. FileAccessRef fb = FileAccess::open(p_files[i], FileAccess::READ);
  93. if (!fb) {
  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. fb->close();
  115. }
  116. return true;
  117. }
  118. bool LipO::open_file(const String &p_path) {
  119. close();
  120. fa = FileAccess::open(p_path, FileAccess::READ);
  121. ERR_FAIL_COND_V_MSG(!fa, false, vformat("LipO: Can't open file: \"%s\".", p_path));
  122. uint32_t magic = fa->get_32();
  123. if (magic == 0xbebafeca) {
  124. // 32-bit fat binary, bswap.
  125. uint32_t nfat_arch = BSWAP32(fa->get_32());
  126. for (uint32_t i = 0; i < nfat_arch; i++) {
  127. FatArch arch;
  128. arch.cputype = BSWAP32(fa->get_32());
  129. arch.cpusubtype = BSWAP32(fa->get_32());
  130. arch.offset = BSWAP32(fa->get_32());
  131. arch.size = BSWAP32(fa->get_32());
  132. arch.align = BSWAP32(fa->get_32());
  133. archs.push_back(arch);
  134. }
  135. } else if (magic == 0xcafebabe) {
  136. // 32-bit fat binary.
  137. uint32_t nfat_arch = fa->get_32();
  138. for (uint32_t i = 0; i < nfat_arch; i++) {
  139. FatArch arch;
  140. arch.cputype = fa->get_32();
  141. arch.cpusubtype = fa->get_32();
  142. arch.offset = fa->get_32();
  143. arch.size = fa->get_32();
  144. arch.align = fa->get_32();
  145. archs.push_back(arch);
  146. }
  147. } else if (magic == 0xbfbafeca) {
  148. // 64-bit fat binary, bswap.
  149. uint32_t nfat_arch = BSWAP32(fa->get_32());
  150. for (uint32_t i = 0; i < nfat_arch; i++) {
  151. FatArch arch;
  152. arch.cputype = BSWAP32(fa->get_32());
  153. arch.cpusubtype = BSWAP32(fa->get_32());
  154. arch.offset = BSWAP64(fa->get_64());
  155. arch.size = BSWAP64(fa->get_64());
  156. arch.align = BSWAP32(fa->get_32());
  157. fa->get_32(); // Skip, reserved.
  158. archs.push_back(arch);
  159. }
  160. } else if (magic == 0xcafebabf) {
  161. // 64-bit fat binary.
  162. uint32_t nfat_arch = fa->get_32();
  163. for (uint32_t i = 0; i < nfat_arch; i++) {
  164. FatArch arch;
  165. arch.cputype = fa->get_32();
  166. arch.cpusubtype = fa->get_32();
  167. arch.offset = fa->get_64();
  168. arch.size = fa->get_64();
  169. arch.align = fa->get_32();
  170. fa->get_32(); // Skip, reserved.
  171. archs.push_back(arch);
  172. }
  173. } else {
  174. close();
  175. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid fat binary: \"%s\".", p_path));
  176. }
  177. return true;
  178. }
  179. int LipO::get_arch_count() const {
  180. ERR_FAIL_COND_V_MSG(!fa, 0, "LipO: File not opened.");
  181. return archs.size();
  182. }
  183. bool LipO::extract_arch(int p_index, const String &p_path) {
  184. ERR_FAIL_COND_V_MSG(!fa, false, "LipO: File not opened.");
  185. ERR_FAIL_INDEX_V(p_index, archs.size(), false);
  186. FileAccessRef fb = FileAccess::open(p_path, FileAccess::WRITE);
  187. ERR_FAIL_COND_V_MSG(!fb, false, vformat("LipO: Can't open file: \"%s\".", p_path));
  188. fa->seek(archs[p_index].offset);
  189. int pages = archs[p_index].size / 4096;
  190. int remain = archs[p_index].size % 4096;
  191. unsigned char step[4096];
  192. for (int i = 0; i < pages; i++) {
  193. uint64_t br = fa->get_buffer(step, 4096);
  194. if (br > 0) {
  195. fb->store_buffer(step, br);
  196. }
  197. }
  198. uint64_t br = fa->get_buffer(step, remain);
  199. if (br > 0) {
  200. fb->store_buffer(step, br);
  201. }
  202. fb->close();
  203. return true;
  204. }
  205. void LipO::close() {
  206. if (fa) {
  207. fa->close();
  208. memdelete(fa);
  209. fa = nullptr;
  210. }
  211. archs.clear();
  212. }
  213. LipO::~LipO() {
  214. close();
  215. }
  216. #endif // MODULE_REGEX_ENABLED