pck_packer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**************************************************************************/
  2. /* pck_packer.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 "pck_packer.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/file_access_encrypted.h"
  34. #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
  35. #include "core/version.h"
  36. static int _get_pad(int p_alignment, int p_n) {
  37. int rest = p_n % p_alignment;
  38. int pad = 0;
  39. if (rest > 0) {
  40. pad = p_alignment - rest;
  41. }
  42. return pad;
  43. }
  44. void PCKPacker::_bind_methods() {
  45. ClassDB::bind_method(D_METHOD("pck_start", "pck_path", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(32), DEFVAL("0000000000000000000000000000000000000000000000000000000000000000"), DEFVAL(false));
  46. ClassDB::bind_method(D_METHOD("add_file", "target_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false));
  47. ClassDB::bind_method(D_METHOD("add_file_removal", "target_path"), &PCKPacker::add_file_removal);
  48. ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false));
  49. }
  50. Error PCKPacker::pck_start(const String &p_pck_path, int p_alignment, const String &p_key, bool p_encrypt_directory) {
  51. ERR_FAIL_COND_V_MSG((p_key.is_empty() || !p_key.is_valid_hex_number(false) || p_key.length() != 64), ERR_CANT_CREATE, "Invalid Encryption Key (must be 64 characters long).");
  52. ERR_FAIL_COND_V_MSG(p_alignment <= 0, ERR_CANT_CREATE, "Invalid alignment, must be greater then 0.");
  53. String _key = p_key.to_lower();
  54. key.resize(32);
  55. for (int i = 0; i < 32; i++) {
  56. int v = 0;
  57. if (i * 2 < _key.length()) {
  58. char32_t ct = _key[i * 2];
  59. if (is_digit(ct)) {
  60. ct = ct - '0';
  61. } else if (ct >= 'a' && ct <= 'f') {
  62. ct = 10 + ct - 'a';
  63. }
  64. v |= ct << 4;
  65. }
  66. if (i * 2 + 1 < _key.length()) {
  67. char32_t ct = _key[i * 2 + 1];
  68. if (is_digit(ct)) {
  69. ct = ct - '0';
  70. } else if (ct >= 'a' && ct <= 'f') {
  71. ct = 10 + ct - 'a';
  72. }
  73. v |= ct;
  74. }
  75. key.write[i] = v;
  76. }
  77. enc_dir = p_encrypt_directory;
  78. file = FileAccess::open(p_pck_path, FileAccess::WRITE);
  79. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_CANT_CREATE, vformat("Can't open file to write: '%s'.", String(p_pck_path)));
  80. alignment = p_alignment;
  81. file->store_32(PACK_HEADER_MAGIC);
  82. file->store_32(PACK_FORMAT_VERSION);
  83. file->store_32(GODOT_VERSION_MAJOR);
  84. file->store_32(GODOT_VERSION_MINOR);
  85. file->store_32(GODOT_VERSION_PATCH);
  86. uint32_t pack_flags = PACK_REL_FILEBASE;
  87. if (enc_dir) {
  88. pack_flags |= PACK_DIR_ENCRYPTED;
  89. }
  90. file->store_32(pack_flags); // flags
  91. file_base_ofs = file->get_position();
  92. file->store_64(0); // Files base.
  93. dir_base_ofs = file->get_position();
  94. file->store_64(0); // Directory offset.
  95. for (int i = 0; i < 16; i++) {
  96. file->store_32(0); // Reserved.
  97. }
  98. // Align for first file.
  99. int pad = _get_pad(alignment, file->get_position());
  100. for (int i = 0; i < pad; i++) {
  101. file->store_8(0);
  102. }
  103. file_base = file->get_position();
  104. file->seek(file_base_ofs);
  105. file->store_64(file_base); // Update files base.
  106. file->seek(file_base);
  107. files.clear();
  108. return OK;
  109. }
  110. Error PCKPacker::add_file_removal(const String &p_target_path) {
  111. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  112. File pf;
  113. // Simplify path here and on every 'files' access so that paths that have extra '/'
  114. // symbols or 'res://' in them still match the MD5 hash for the saved path.
  115. pf.path = p_target_path.simplify_path().trim_prefix("res://");
  116. pf.ofs = file->get_position();
  117. pf.size = 0;
  118. pf.removal = true;
  119. pf.md5.resize_initialized(16);
  120. files.push_back(pf);
  121. return OK;
  122. }
  123. Error PCKPacker::add_file(const String &p_target_path, const String &p_source_path, bool p_encrypt) {
  124. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  125. Ref<FileAccess> f = FileAccess::open(p_source_path, FileAccess::READ);
  126. if (f.is_null()) {
  127. return ERR_FILE_CANT_OPEN;
  128. }
  129. File pf;
  130. // Simplify path here and on every 'files' access so that paths that have extra '/'
  131. // symbols or 'res://' in them still match the MD5 hash for the saved path.
  132. pf.path = p_target_path.simplify_path().trim_prefix("res://");
  133. pf.src_path = p_source_path;
  134. pf.ofs = file->get_position();
  135. pf.size = f->get_length();
  136. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_source_path);
  137. {
  138. unsigned char hash[16];
  139. CryptoCore::md5(data.ptr(), data.size(), hash);
  140. pf.md5.resize(16);
  141. for (int i = 0; i < 16; i++) {
  142. pf.md5.write[i] = hash[i];
  143. }
  144. }
  145. pf.encrypted = p_encrypt;
  146. Ref<FileAccess> ftmp = file;
  147. Ref<FileAccessEncrypted> fae;
  148. if (p_encrypt) {
  149. fae.instantiate();
  150. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  151. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  152. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  153. ftmp = fae;
  154. }
  155. ftmp->store_buffer(data);
  156. if (fae.is_valid()) {
  157. ftmp.unref();
  158. fae.unref();
  159. }
  160. int pad = _get_pad(alignment, file->get_position());
  161. for (int j = 0; j < pad; j++) {
  162. file->store_8(0);
  163. }
  164. files.push_back(pf);
  165. return OK;
  166. }
  167. Error PCKPacker::flush(bool p_verbose) {
  168. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  169. int dir_padding = _get_pad(alignment, file->get_position());
  170. for (int i = 0; i < dir_padding; i++) {
  171. file->store_8(0);
  172. }
  173. // Write directory.
  174. uint64_t dir_offset = file->get_position();
  175. file->seek(dir_base_ofs);
  176. file->store_64(dir_offset);
  177. file->seek(dir_offset);
  178. file->store_32(uint32_t(files.size()));
  179. Ref<FileAccessEncrypted> fae;
  180. Ref<FileAccess> fhead = file;
  181. if (enc_dir) {
  182. fae.instantiate();
  183. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  184. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  185. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  186. fhead = fae;
  187. }
  188. const int file_num = files.size();
  189. for (int i = 0; i < file_num; i++) {
  190. CharString utf8_string = files[i].path.utf8();
  191. int string_len = utf8_string.length();
  192. int pad = _get_pad(4, string_len);
  193. fhead->store_32(uint32_t(string_len + pad));
  194. fhead->store_buffer((const uint8_t *)utf8_string.get_data(), string_len);
  195. for (int j = 0; j < pad; j++) {
  196. fhead->store_8(0);
  197. }
  198. fhead->store_64(files[i].ofs - file_base);
  199. fhead->store_64(files[i].size);
  200. fhead->store_buffer(files[i].md5.ptr(), 16);
  201. uint32_t flags = 0;
  202. if (files[i].encrypted) {
  203. flags |= PACK_FILE_ENCRYPTED;
  204. }
  205. if (files[i].removal) {
  206. flags |= PACK_FILE_REMOVAL;
  207. }
  208. fhead->store_32(flags);
  209. if (p_verbose) {
  210. print_line(vformat("[%d/%d - %d%%] PCKPacker flush: %s -> %s", i, file_num, float(i) / file_num * 100, files[i].src_path, files[i].path));
  211. }
  212. }
  213. if (fae.is_valid()) {
  214. fhead.unref();
  215. fae.unref();
  216. }
  217. file.unref();
  218. return OK;
  219. }
  220. PCKPacker::~PCKPacker() {
  221. if (file.is_valid()) {
  222. flush();
  223. }
  224. }