export_plugin.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*************************************************************************/
  2. /* export_plugin.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 "export_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  34. if (p_preset->get("codesign/enable")) {
  35. return _code_sign(p_preset, p_path);
  36. } else {
  37. return OK;
  38. }
  39. }
  40. Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  41. if (p_preset->get("application/modify_resources")) {
  42. _rcedit_add_data(p_preset, p_path, true);
  43. String wrapper_path = p_path.get_basename() + ".console.exe";
  44. if (FileAccess::exists(wrapper_path)) {
  45. _rcedit_add_data(p_preset, wrapper_path, false);
  46. }
  47. }
  48. return OK;
  49. }
  50. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  51. String pck_path = p_path;
  52. if (p_preset->get("binary_format/embed_pck")) {
  53. pck_path = p_path.get_basename() + ".tmp";
  54. }
  55. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, pck_path, p_flags);
  56. if (p_preset->get("codesign/enable") && err == OK) {
  57. _code_sign(p_preset, pck_path);
  58. String wrapper_path = p_path.get_basename() + ".console.exe";
  59. if (FileAccess::exists(wrapper_path)) {
  60. _code_sign(p_preset, wrapper_path);
  61. }
  62. }
  63. if (p_preset->get("binary_format/embed_pck") && err == OK) {
  64. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  65. err = tmp_dir->rename(pck_path, p_path);
  66. if (err != OK) {
  67. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to rename temporary file \"%s\"."), pck_path));
  68. }
  69. }
  70. return err;
  71. }
  72. String EditorExportPlatformWindows::get_template_file_name(const String &p_target, const String &p_arch) const {
  73. return "windows_" + p_target + "_" + p_arch + ".exe";
  74. }
  75. List<String> EditorExportPlatformWindows::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  76. List<String> list;
  77. list.push_back("exe");
  78. return list;
  79. }
  80. bool EditorExportPlatformWindows::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  81. // This option is not supported by "osslsigncode", used on non-Windows host.
  82. if (!OS::get_singleton()->has_feature("windows") && p_option == "codesign/identity_type") {
  83. return false;
  84. }
  85. return true;
  86. }
  87. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  88. EditorExportPlatformPC::get_export_options(r_options);
  89. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64"), "x86_64"));
  90. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  91. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/identity_type", PROPERTY_HINT_ENUM, "Select automatically,Use PKCS12 file (specify *.PFX/*.P12 file),Use certificate store (specify SHA1 hash)"), 0));
  92. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12"), ""));
  93. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password", PROPERTY_HINT_PASSWORD), ""));
  94. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  95. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), ""));
  96. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1));
  97. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), ""));
  98. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  99. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/modify_resources"), true));
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico"), ""));
  101. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/console_wrapper_icon", PROPERTY_HINT_FILE, "*.ico"), ""));
  102. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  103. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  104. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  105. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  106. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  107. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  108. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  109. }
  110. Error EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path, bool p_console_icon) {
  111. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  112. if (rcedit_path != String() && !FileAccess::exists(rcedit_path)) {
  113. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find rcedit executable at \"%s\"."), rcedit_path));
  114. return ERR_FILE_NOT_FOUND;
  115. }
  116. if (rcedit_path == String()) {
  117. rcedit_path = "rcedit"; // try to run rcedit from PATH
  118. }
  119. #ifndef WINDOWS_ENABLED
  120. // On non-Windows we need WINE to run rcedit
  121. String wine_path = EDITOR_GET("export/windows/wine");
  122. if (!wine_path.is_empty() && !FileAccess::exists(wine_path)) {
  123. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find wine executable at \"%s\"."), wine_path));
  124. return ERR_FILE_NOT_FOUND;
  125. }
  126. if (wine_path.is_empty()) {
  127. wine_path = "wine"; // try to run wine from PATH
  128. }
  129. #endif
  130. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  131. if (p_console_icon) {
  132. String console_icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/console_wrapper_icon"));
  133. if (!console_icon_path.is_empty() && FileAccess::exists(console_icon_path)) {
  134. icon_path = console_icon_path;
  135. }
  136. }
  137. String file_verion = p_preset->get("application/file_version");
  138. String product_version = p_preset->get("application/product_version");
  139. String company_name = p_preset->get("application/company_name");
  140. String product_name = p_preset->get("application/product_name");
  141. String file_description = p_preset->get("application/file_description");
  142. String copyright = p_preset->get("application/copyright");
  143. String trademarks = p_preset->get("application/trademarks");
  144. String comments = p_preset->get("application/comments");
  145. List<String> args;
  146. args.push_back(p_path);
  147. if (!icon_path.is_empty()) {
  148. args.push_back("--set-icon");
  149. args.push_back(icon_path);
  150. }
  151. if (!file_verion.is_empty()) {
  152. args.push_back("--set-file-version");
  153. args.push_back(file_verion);
  154. }
  155. if (!product_version.is_empty()) {
  156. args.push_back("--set-product-version");
  157. args.push_back(product_version);
  158. }
  159. if (!company_name.is_empty()) {
  160. args.push_back("--set-version-string");
  161. args.push_back("CompanyName");
  162. args.push_back(company_name);
  163. }
  164. if (!product_name.is_empty()) {
  165. args.push_back("--set-version-string");
  166. args.push_back("ProductName");
  167. args.push_back(product_name);
  168. }
  169. if (!file_description.is_empty()) {
  170. args.push_back("--set-version-string");
  171. args.push_back("FileDescription");
  172. args.push_back(file_description);
  173. }
  174. if (!copyright.is_empty()) {
  175. args.push_back("--set-version-string");
  176. args.push_back("LegalCopyright");
  177. args.push_back(copyright);
  178. }
  179. if (!trademarks.is_empty()) {
  180. args.push_back("--set-version-string");
  181. args.push_back("LegalTrademarks");
  182. args.push_back(trademarks);
  183. }
  184. #ifndef WINDOWS_ENABLED
  185. // On non-Windows we need WINE to run rcedit
  186. args.push_front(rcedit_path);
  187. rcedit_path = wine_path;
  188. #endif
  189. String str;
  190. Error err = OS::get_singleton()->execute(rcedit_path, args, &str, nullptr, true);
  191. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  192. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), TTR("Could not start rcedit executable. Configure rcedit path in the Editor Settings (Export > Windows > rcedit), or disable \"Application > Modify Resources\" in the export preset."));
  193. return err;
  194. }
  195. print_line("rcedit (" + p_path + "): " + str);
  196. if (str.find("Fatal error") != -1) {
  197. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("rcedit failed to modify executable: %s."), str));
  198. return FAILED;
  199. }
  200. return OK;
  201. }
  202. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  203. List<String> args;
  204. #ifdef WINDOWS_ENABLED
  205. String signtool_path = EDITOR_GET("export/windows/signtool");
  206. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  207. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find signtool executable at \"%s\"."), signtool_path));
  208. return ERR_FILE_NOT_FOUND;
  209. }
  210. if (signtool_path.is_empty()) {
  211. signtool_path = "signtool"; // try to run signtool from PATH
  212. }
  213. #else
  214. String signtool_path = EDITOR_GET("export/windows/osslsigncode");
  215. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  216. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find osslsigncode executable at \"%s\"."), signtool_path));
  217. return ERR_FILE_NOT_FOUND;
  218. }
  219. if (signtool_path.is_empty()) {
  220. signtool_path = "osslsigncode"; // try to run signtool from PATH
  221. }
  222. #endif
  223. args.push_back("sign");
  224. //identity
  225. #ifdef WINDOWS_ENABLED
  226. int id_type = p_preset->get("codesign/identity_type");
  227. if (id_type == 0) { //auto select
  228. args.push_back("/a");
  229. } else if (id_type == 1) { //pkcs12
  230. if (p_preset->get("codesign/identity") != "") {
  231. args.push_back("/f");
  232. args.push_back(p_preset->get("codesign/identity"));
  233. } else {
  234. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  235. return FAILED;
  236. }
  237. } else if (id_type == 2) { //Windows certificate store
  238. if (p_preset->get("codesign/identity") != "") {
  239. args.push_back("/sha1");
  240. args.push_back(p_preset->get("codesign/identity"));
  241. } else {
  242. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  243. return FAILED;
  244. }
  245. } else {
  246. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid identity type."));
  247. return FAILED;
  248. }
  249. #else
  250. if (p_preset->get("codesign/identity") != "") {
  251. args.push_back("-pkcs12");
  252. args.push_back(p_preset->get("codesign/identity"));
  253. } else {
  254. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  255. return FAILED;
  256. }
  257. #endif
  258. //password
  259. if (p_preset->get("codesign/password") != "") {
  260. #ifdef WINDOWS_ENABLED
  261. args.push_back("/p");
  262. #else
  263. args.push_back("-pass");
  264. #endif
  265. args.push_back(p_preset->get("codesign/password"));
  266. }
  267. //timestamp
  268. if (p_preset->get("codesign/timestamp")) {
  269. if (p_preset->get("codesign/timestamp_server") != "") {
  270. #ifdef WINDOWS_ENABLED
  271. args.push_back("/tr");
  272. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  273. args.push_back("/td");
  274. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  275. args.push_back("sha1");
  276. } else {
  277. args.push_back("sha256");
  278. }
  279. #else
  280. args.push_back("-ts");
  281. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  282. #endif
  283. } else {
  284. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid timestamp server."));
  285. return FAILED;
  286. }
  287. }
  288. //digest
  289. #ifdef WINDOWS_ENABLED
  290. args.push_back("/fd");
  291. #else
  292. args.push_back("-h");
  293. #endif
  294. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  295. args.push_back("sha1");
  296. } else {
  297. args.push_back("sha256");
  298. }
  299. //description
  300. if (p_preset->get("codesign/description") != "") {
  301. #ifdef WINDOWS_ENABLED
  302. args.push_back("/d");
  303. #else
  304. args.push_back("-n");
  305. #endif
  306. args.push_back(p_preset->get("codesign/description"));
  307. }
  308. //user options
  309. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  310. for (int i = 0; i < user_args.size(); i++) {
  311. String user_arg = user_args[i].strip_edges();
  312. if (!user_arg.is_empty()) {
  313. args.push_back(user_arg);
  314. }
  315. }
  316. #ifndef WINDOWS_ENABLED
  317. args.push_back("-in");
  318. #endif
  319. args.push_back(p_path);
  320. #ifndef WINDOWS_ENABLED
  321. args.push_back("-out");
  322. args.push_back(p_path + "_signed");
  323. #endif
  324. String str;
  325. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  326. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  327. #ifndef WINDOWS_ENABLED
  328. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start signtool executable. Configure signtool path in the Editor Settings (Export > Windows > signtool), or disable \"Codesign\" in the export preset."));
  329. #else
  330. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start osslsigncode executable. Configure signtool path in the Editor Settings (Export > Windows > osslsigncode), or disable \"Codesign\" in the export preset."));
  331. #endif
  332. return err;
  333. }
  334. print_line("codesign (" + p_path + "): " + str);
  335. #ifndef WINDOWS_ENABLED
  336. if (str.find("SignTool Error") != -1) {
  337. #else
  338. if (str.find("Failed") != -1) {
  339. #endif
  340. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Signtool failed to sign executable: %s."), str));
  341. return FAILED;
  342. }
  343. #ifndef WINDOWS_ENABLED
  344. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  345. err = tmp_dir->remove(p_path);
  346. if (err != OK) {
  347. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to remove temporary file \"%s\"."), p_path));
  348. return err;
  349. }
  350. err = tmp_dir->rename(p_path + "_signed", p_path);
  351. if (err != OK) {
  352. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to rename temporary file \"%s\"."), p_path + "_signed"));
  353. return err;
  354. }
  355. #endif
  356. return OK;
  357. }
  358. bool EditorExportPlatformWindows::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  359. String err = "";
  360. bool valid = EditorExportPlatformPC::has_valid_export_configuration(p_preset, err, r_missing_templates);
  361. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  362. if (p_preset->get("application/modify_resources") && rcedit_path.is_empty()) {
  363. err += TTR("The rcedit tool must be configured in the Editor Settings (Export > Windows > rcedit) to change the icon or app information data.") + "\n";
  364. }
  365. if (!err.is_empty()) {
  366. r_error = err;
  367. }
  368. return valid;
  369. }
  370. bool EditorExportPlatformWindows::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  371. String err = "";
  372. bool valid = true;
  373. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  374. if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) {
  375. err += TTR("Invalid icon path:") + " " + icon_path + "\n";
  376. }
  377. // Only non-negative integers can exist in the version string.
  378. String file_version = p_preset->get("application/file_version");
  379. if (!file_version.is_empty()) {
  380. PackedStringArray version_array = file_version.split(".", false);
  381. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  382. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  383. !version_array[3].is_valid_int() || file_version.find("-") > -1) {
  384. err += TTR("Invalid file version:") + " " + file_version + "\n";
  385. }
  386. }
  387. String product_version = p_preset->get("application/product_version");
  388. if (!product_version.is_empty()) {
  389. PackedStringArray version_array = product_version.split(".", false);
  390. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  391. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  392. !version_array[3].is_valid_int() || product_version.find("-") > -1) {
  393. err += TTR("Invalid product version:") + " " + product_version + "\n";
  394. }
  395. }
  396. if (!err.is_empty()) {
  397. r_error = err;
  398. }
  399. return valid;
  400. }
  401. Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  402. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  403. if (p_embedded_size + p_embedded_start >= 0x100000000) { // Check for total executable size
  404. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Windows executables cannot be >= 4 GiB."));
  405. return ERR_INVALID_DATA;
  406. }
  407. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  408. if (f.is_null()) {
  409. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path));
  410. return ERR_CANT_OPEN;
  411. }
  412. // Jump to the PE header and check the magic number
  413. {
  414. f->seek(0x3c);
  415. uint32_t pe_pos = f->get_32();
  416. f->seek(pe_pos);
  417. uint32_t magic = f->get_32();
  418. if (magic != 0x00004550) {
  419. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted."));
  420. return ERR_FILE_CORRUPT;
  421. }
  422. }
  423. // Process header
  424. int num_sections;
  425. {
  426. int64_t header_pos = f->get_position();
  427. f->seek(header_pos + 2);
  428. num_sections = f->get_16();
  429. f->seek(header_pos + 16);
  430. uint16_t opt_header_size = f->get_16();
  431. // Skip rest of header + optional header to go to the section headers
  432. f->seek(f->get_position() + 2 + opt_header_size);
  433. }
  434. // Search for the "pck" section
  435. int64_t section_table_pos = f->get_position();
  436. bool found = false;
  437. for (int i = 0; i < num_sections; ++i) {
  438. int64_t section_header_pos = section_table_pos + i * 40;
  439. f->seek(section_header_pos);
  440. uint8_t section_name[9];
  441. f->get_buffer(section_name, 8);
  442. section_name[8] = '\0';
  443. if (strcmp((char *)section_name, "pck") == 0) {
  444. // "pck" section found, let's patch!
  445. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  446. f->seek(section_header_pos + 8);
  447. f->store_32(8);
  448. f->seek(section_header_pos + 16);
  449. f->store_32(p_embedded_size);
  450. f->seek(section_header_pos + 20);
  451. f->store_32(p_embedded_start);
  452. found = true;
  453. break;
  454. }
  455. }
  456. if (!found) {
  457. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found."));
  458. return ERR_FILE_CORRUPT;
  459. }
  460. return OK;
  461. }