pck_packer.cpp 8.2 KB

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