pck_packer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_name", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(32), DEFVAL("0000000000000000000000000000000000000000000000000000000000000000"), DEFVAL(false));
  46. ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false));
  47. ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false));
  48. }
  49. Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String &p_key, bool p_encrypt_directory) {
  50. 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).");
  51. ERR_FAIL_COND_V_MSG(p_alignment <= 0, ERR_CANT_CREATE, "Invalid alignment, must be greater then 0.");
  52. String _key = p_key.to_lower();
  53. key.resize(32);
  54. for (int i = 0; i < 32; i++) {
  55. int v = 0;
  56. if (i * 2 < _key.length()) {
  57. char32_t ct = _key[i * 2];
  58. if (is_digit(ct)) {
  59. ct = ct - '0';
  60. } else if (ct >= 'a' && ct <= 'f') {
  61. ct = 10 + ct - 'a';
  62. }
  63. v |= ct << 4;
  64. }
  65. if (i * 2 + 1 < _key.length()) {
  66. char32_t ct = _key[i * 2 + 1];
  67. if (is_digit(ct)) {
  68. ct = ct - '0';
  69. } else if (ct >= 'a' && ct <= 'f') {
  70. ct = 10 + ct - 'a';
  71. }
  72. v |= ct;
  73. }
  74. key.write[i] = v;
  75. }
  76. enc_dir = p_encrypt_directory;
  77. file = FileAccess::open(p_file, FileAccess::WRITE);
  78. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_CANT_CREATE, "Can't open file to write: " + String(p_file) + ".");
  79. alignment = p_alignment;
  80. file->store_32(PACK_HEADER_MAGIC);
  81. file->store_32(PACK_FORMAT_VERSION);
  82. file->store_32(VERSION_MAJOR);
  83. file->store_32(VERSION_MINOR);
  84. file->store_32(VERSION_PATCH);
  85. uint32_t pack_flags = 0;
  86. if (enc_dir) {
  87. pack_flags |= PACK_DIR_ENCRYPTED;
  88. }
  89. file->store_32(pack_flags); // flags
  90. files.clear();
  91. ofs = 0;
  92. return OK;
  93. }
  94. Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) {
  95. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  96. Ref<FileAccess> f = FileAccess::open(p_src, FileAccess::READ);
  97. if (f.is_null()) {
  98. return ERR_FILE_CANT_OPEN;
  99. }
  100. File pf;
  101. // Simplify path here and on every 'files' access so that paths that have extra '/'
  102. // symbols in them still match to the MD5 hash for the saved path.
  103. pf.path = p_file.simplify_path();
  104. pf.src_path = p_src;
  105. pf.ofs = ofs;
  106. pf.size = f->get_length();
  107. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_src);
  108. {
  109. unsigned char hash[16];
  110. CryptoCore::md5(data.ptr(), data.size(), hash);
  111. pf.md5.resize(16);
  112. for (int i = 0; i < 16; i++) {
  113. pf.md5.write[i] = hash[i];
  114. }
  115. }
  116. pf.encrypted = p_encrypt;
  117. uint64_t _size = pf.size;
  118. if (p_encrypt) { // Add encryption overhead.
  119. if (_size % 16) { // Pad to encryption block size.
  120. _size += 16 - (_size % 16);
  121. }
  122. _size += 16; // hash
  123. _size += 8; // data size
  124. _size += 16; // iv
  125. }
  126. int pad = _get_pad(alignment, ofs + _size);
  127. ofs = ofs + _size + pad;
  128. files.push_back(pf);
  129. return OK;
  130. }
  131. Error PCKPacker::flush(bool p_verbose) {
  132. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  133. int64_t file_base_ofs = file->get_position();
  134. file->store_64(0); // files base
  135. for (int i = 0; i < 16; i++) {
  136. file->store_32(0); // reserved
  137. }
  138. // write the index
  139. file->store_32(files.size());
  140. Ref<FileAccessEncrypted> fae;
  141. Ref<FileAccess> fhead = file;
  142. if (enc_dir) {
  143. fae.instantiate();
  144. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  145. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  146. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  147. fhead = fae;
  148. }
  149. for (int i = 0; i < files.size(); i++) {
  150. int string_len = files[i].path.utf8().length();
  151. int pad = _get_pad(4, string_len);
  152. fhead->store_32(string_len + pad);
  153. fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len);
  154. for (int j = 0; j < pad; j++) {
  155. fhead->store_8(0);
  156. }
  157. fhead->store_64(files[i].ofs);
  158. fhead->store_64(files[i].size); // pay attention here, this is where file is
  159. fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file
  160. uint32_t flags = 0;
  161. if (files[i].encrypted) {
  162. flags |= PACK_FILE_ENCRYPTED;
  163. }
  164. fhead->store_32(flags);
  165. }
  166. if (fae.is_valid()) {
  167. fhead.unref();
  168. fae.unref();
  169. }
  170. int header_padding = _get_pad(alignment, file->get_position());
  171. for (int i = 0; i < header_padding; i++) {
  172. file->store_8(0);
  173. }
  174. int64_t file_base = file->get_position();
  175. file->seek(file_base_ofs);
  176. file->store_64(file_base); // update files base
  177. file->seek(file_base);
  178. const uint32_t buf_max = 65536;
  179. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  180. int count = 0;
  181. for (int i = 0; i < files.size(); i++) {
  182. Ref<FileAccess> src = FileAccess::open(files[i].src_path, FileAccess::READ);
  183. uint64_t to_write = files[i].size;
  184. Ref<FileAccess> ftmp = file;
  185. if (files[i].encrypted) {
  186. fae.instantiate();
  187. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  188. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  189. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  190. ftmp = fae;
  191. }
  192. while (to_write > 0) {
  193. uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max));
  194. ftmp->store_buffer(buf, read);
  195. to_write -= read;
  196. }
  197. if (fae.is_valid()) {
  198. ftmp.unref();
  199. fae.unref();
  200. }
  201. int pad = _get_pad(alignment, file->get_position());
  202. for (int j = 0; j < pad; j++) {
  203. file->store_8(0);
  204. }
  205. count += 1;
  206. const int file_num = files.size();
  207. if (p_verbose && (file_num > 0)) {
  208. 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));
  209. }
  210. }
  211. file.unref();
  212. memdelete_arr(buf);
  213. return OK;
  214. }