export_plugin.cpp 20 KB

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