pck_packer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(VERSION_MAJOR);
  84. file->store_32(VERSION_MINOR);
  85. file->store_32(VERSION_PATCH);
  86. uint32_t pack_flags = 0;
  87. if (enc_dir) {
  88. pack_flags |= PACK_DIR_ENCRYPTED;
  89. }
  90. file->store_32(pack_flags); // flags
  91. files.clear();
  92. ofs = 0;
  93. return OK;
  94. }
  95. Error PCKPacker::add_file_removal(const String &p_target_path) {
  96. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  97. File pf;
  98. // Simplify path here and on every 'files' access so that paths that have extra '/'
  99. // symbols or 'res://' in them still match the MD5 hash for the saved path.
  100. pf.path = p_target_path.simplify_path().trim_prefix("res://");
  101. pf.ofs = ofs;
  102. pf.size = 0;
  103. pf.removal = true;
  104. pf.md5.resize(16);
  105. pf.md5.fill(0);
  106. files.push_back(pf);
  107. return OK;
  108. }
  109. Error PCKPacker::add_file(const String &p_target_path, const String &p_source_path, bool p_encrypt) {
  110. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  111. Ref<FileAccess> f = FileAccess::open(p_source_path, FileAccess::READ);
  112. if (f.is_null()) {
  113. return ERR_FILE_CANT_OPEN;
  114. }
  115. File pf;
  116. // Simplify path here and on every 'files' access so that paths that have extra '/'
  117. // symbols or 'res://' in them still match the MD5 hash for the saved path.
  118. pf.path = p_target_path.simplify_path().trim_prefix("res://");
  119. pf.src_path = p_source_path;
  120. pf.ofs = ofs;
  121. pf.size = f->get_length();
  122. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_source_path);
  123. {
  124. unsigned char hash[16];
  125. CryptoCore::md5(data.ptr(), data.size(), hash);
  126. pf.md5.resize(16);
  127. for (int i = 0; i < 16; i++) {
  128. pf.md5.write[i] = hash[i];
  129. }
  130. }
  131. pf.encrypted = p_encrypt;
  132. uint64_t _size = pf.size;
  133. if (p_encrypt) { // Add encryption overhead.
  134. if (_size % 16) { // Pad to encryption block size.
  135. _size += 16 - (_size % 16);
  136. }
  137. _size += 16; // hash
  138. _size += 8; // data size
  139. _size += 16; // iv
  140. }
  141. int pad = _get_pad(alignment, ofs + _size);
  142. ofs = ofs + _size + pad;
  143. files.push_back(pf);
  144. return OK;
  145. }
  146. Error PCKPacker::flush(bool p_verbose) {
  147. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  148. int64_t file_base_ofs = file->get_position();
  149. file->store_64(0); // files base
  150. for (int i = 0; i < 16; i++) {
  151. file->store_32(0); // reserved
  152. }
  153. // write the index
  154. file->store_32(uint32_t(files.size()));
  155. Ref<FileAccessEncrypted> fae;
  156. Ref<FileAccess> fhead = file;
  157. if (enc_dir) {
  158. fae.instantiate();
  159. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  160. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  161. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  162. fhead = fae;
  163. }
  164. for (int i = 0; i < files.size(); i++) {
  165. CharString utf8_string = files[i].path.utf8();
  166. int string_len = utf8_string.length();
  167. int pad = _get_pad(4, string_len);
  168. fhead->store_32(uint32_t(string_len + pad));
  169. fhead->store_buffer((const uint8_t *)utf8_string.get_data(), string_len);
  170. for (int j = 0; j < pad; j++) {
  171. fhead->store_8(0);
  172. }
  173. fhead->store_64(files[i].ofs);
  174. fhead->store_64(files[i].size); // pay attention here, this is where file is
  175. fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file
  176. uint32_t flags = 0;
  177. if (files[i].encrypted) {
  178. flags |= PACK_FILE_ENCRYPTED;
  179. }
  180. if (files[i].removal) {
  181. flags |= PACK_FILE_REMOVAL;
  182. }
  183. fhead->store_32(flags);
  184. }
  185. if (fae.is_valid()) {
  186. fhead.unref();
  187. fae.unref();
  188. }
  189. int header_padding = _get_pad(alignment, file->get_position());
  190. for (int i = 0; i < header_padding; i++) {
  191. file->store_8(0);
  192. }
  193. uint64_t file_base = file->get_position();
  194. file->seek(file_base_ofs);
  195. file->store_64(file_base); // update files base
  196. file->seek(file_base);
  197. const uint32_t buf_max = 65536;
  198. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  199. int count = 0;
  200. for (int i = 0; i < files.size(); i++) {
  201. if (files[i].removal) {
  202. continue;
  203. }
  204. Ref<FileAccess> src = FileAccess::open(files[i].src_path, FileAccess::READ);
  205. uint64_t to_write = files[i].size;
  206. Ref<FileAccess> ftmp = file;
  207. if (files[i].encrypted) {
  208. fae.instantiate();
  209. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  210. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  211. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  212. ftmp = fae;
  213. }
  214. while (to_write > 0) {
  215. uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max));
  216. ftmp->store_buffer(buf, read);
  217. to_write -= read;
  218. }
  219. if (fae.is_valid()) {
  220. ftmp.unref();
  221. fae.unref();
  222. }
  223. int pad = _get_pad(alignment, file->get_position());
  224. for (int j = 0; j < pad; j++) {
  225. file->store_8(0);
  226. }
  227. count += 1;
  228. const int file_num = files.size();
  229. if (p_verbose && (file_num > 0)) {
  230. print_line(vformat("[%d/%d - %d%%] PCKPacker flush: %s -> %s", count, file_num, float(count) / file_num * 100, files[i].src_path, files[i].path));
  231. }
  232. }
  233. file.unref();
  234. memdelete_arr(buf);
  235. return OK;
  236. }