pck_packer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* pck_packer.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 "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. Ref<FileAccess> f = FileAccess::open(p_src, FileAccess::READ);
  96. if (f.is_null()) {
  97. return ERR_FILE_CANT_OPEN;
  98. }
  99. File pf;
  100. pf.path = p_file;
  101. pf.src_path = p_src;
  102. pf.ofs = ofs;
  103. pf.size = f->get_length();
  104. Vector<uint8_t> data = FileAccess::get_file_as_array(p_src);
  105. {
  106. unsigned char hash[16];
  107. CryptoCore::md5(data.ptr(), data.size(), hash);
  108. pf.md5.resize(16);
  109. for (int i = 0; i < 16; i++) {
  110. pf.md5.write[i] = hash[i];
  111. }
  112. }
  113. pf.encrypted = p_encrypt;
  114. uint64_t _size = pf.size;
  115. if (p_encrypt) { // Add encryption overhead.
  116. if (_size % 16) { // Pad to encryption block size.
  117. _size += 16 - (_size % 16);
  118. }
  119. _size += 16; // hash
  120. _size += 8; // data size
  121. _size += 16; // iv
  122. }
  123. int pad = _get_pad(alignment, ofs + _size);
  124. ofs = ofs + _size + pad;
  125. files.push_back(pf);
  126. return OK;
  127. }
  128. Error PCKPacker::flush(bool p_verbose) {
  129. ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
  130. int64_t file_base_ofs = file->get_position();
  131. file->store_64(0); // files base
  132. for (int i = 0; i < 16; i++) {
  133. file->store_32(0); // reserved
  134. }
  135. // write the index
  136. file->store_32(files.size());
  137. Ref<FileAccessEncrypted> fae;
  138. Ref<FileAccess> fhead = file;
  139. if (enc_dir) {
  140. fae.instantiate();
  141. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  142. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  143. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  144. fhead = fae;
  145. }
  146. for (int i = 0; i < files.size(); i++) {
  147. int string_len = files[i].path.utf8().length();
  148. int pad = _get_pad(4, string_len);
  149. fhead->store_32(string_len + pad);
  150. fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len);
  151. for (int j = 0; j < pad; j++) {
  152. fhead->store_8(0);
  153. }
  154. fhead->store_64(files[i].ofs);
  155. fhead->store_64(files[i].size); // pay attention here, this is where file is
  156. fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file
  157. uint32_t flags = 0;
  158. if (files[i].encrypted) {
  159. flags |= PACK_FILE_ENCRYPTED;
  160. }
  161. fhead->store_32(flags);
  162. }
  163. if (fae.is_valid()) {
  164. fhead.unref();
  165. fae.unref();
  166. }
  167. int header_padding = _get_pad(alignment, file->get_position());
  168. for (int i = 0; i < header_padding; i++) {
  169. file->store_8(Math::rand() % 256);
  170. }
  171. int64_t file_base = file->get_position();
  172. file->seek(file_base_ofs);
  173. file->store_64(file_base); // update files base
  174. file->seek(file_base);
  175. const uint32_t buf_max = 65536;
  176. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  177. int count = 0;
  178. for (int i = 0; i < files.size(); i++) {
  179. Ref<FileAccess> src = FileAccess::open(files[i].src_path, FileAccess::READ);
  180. uint64_t to_write = files[i].size;
  181. Ref<FileAccess> ftmp = file;
  182. if (files[i].encrypted) {
  183. fae.instantiate();
  184. ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
  185. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  186. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  187. ftmp = fae;
  188. }
  189. while (to_write > 0) {
  190. uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max));
  191. ftmp->store_buffer(buf, read);
  192. to_write -= read;
  193. }
  194. if (fae.is_valid()) {
  195. ftmp.unref();
  196. fae.unref();
  197. }
  198. int pad = _get_pad(alignment, file->get_position());
  199. for (int j = 0; j < pad; j++) {
  200. file->store_8(Math::rand() % 256);
  201. }
  202. count += 1;
  203. const int file_num = files.size();
  204. if (p_verbose && (file_num > 0)) {
  205. 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));
  206. }
  207. }
  208. if (p_verbose) {
  209. printf("\n");
  210. }
  211. file.unref();
  212. memdelete_arr(buf);
  213. return OK;
  214. }