pck_packer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*************************************************************************/
  2. /* pck_packer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_encrypted.h"
  33. #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
  34. #include "core/os/file_access.h"
  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(0), DEFVAL(String()), 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. String _key = p_key.to_lower();
  52. key.resize(32);
  53. for (int i = 0; i < 32; i++) {
  54. int v = 0;
  55. if (i * 2 < _key.length()) {
  56. char32_t ct = _key[i * 2];
  57. if (ct >= '0' && ct <= '9') {
  58. ct = ct - '0';
  59. } else if (ct >= 'a' && ct <= 'f') {
  60. ct = 10 + ct - 'a';
  61. }
  62. v |= ct << 4;
  63. }
  64. if (i * 2 + 1 < _key.length()) {
  65. char32_t ct = _key[i * 2 + 1];
  66. if (ct >= '0' && ct <= '9') {
  67. ct = ct - '0';
  68. } else if (ct >= 'a' && ct <= 'f') {
  69. ct = 10 + ct - 'a';
  70. }
  71. v |= ct;
  72. }
  73. key.write[i] = v;
  74. }
  75. enc_dir = p_encrypt_directory;
  76. if (file != nullptr) {
  77. memdelete(file);
  78. }
  79. file = FileAccess::open(p_file, FileAccess::WRITE);
  80. ERR_FAIL_COND_V_MSG(!file, ERR_CANT_CREATE, "Can't open file to write: " + String(p_file) + ".");
  81. alignment = p_alignment;
  82. file->store_32(PACK_HEADER_MAGIC);
  83. file->store_32(PACK_FORMAT_VERSION);
  84. file->store_32(VERSION_MAJOR);
  85. file->store_32(VERSION_MINOR);
  86. file->store_32(VERSION_PATCH);
  87. uint32_t pack_flags = 0;
  88. if (enc_dir) {
  89. pack_flags |= PACK_DIR_ENCRYPTED;
  90. }
  91. file->store_32(pack_flags); // flags
  92. files.clear();
  93. ofs = 0;
  94. return OK;
  95. }
  96. Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) {
  97. FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
  98. if (!f) {
  99. return ERR_FILE_CANT_OPEN;
  100. }
  101. File pf;
  102. pf.path = p_file;
  103. pf.src_path = p_src;
  104. pf.ofs = ofs;
  105. pf.size = f->get_len();
  106. Vector<uint8_t> data = FileAccess::get_file_as_array(p_src);
  107. {
  108. unsigned char hash[16];
  109. CryptoCore::md5(data.ptr(), data.size(), hash);
  110. pf.md5.resize(16);
  111. for (int i = 0; i < 16; i++) {
  112. pf.md5.write[i] = hash[i];
  113. }
  114. }
  115. pf.encrypted = p_encrypt;
  116. uint64_t _size = pf.size;
  117. if (p_encrypt) { // Add encryption overhead.
  118. if (_size % 16) { // Pad to encryption block size.
  119. _size += 16 - (_size % 16);
  120. }
  121. _size += 16; // hash
  122. _size += 8; // data size
  123. _size += 16; // iv
  124. }
  125. int pad = _get_pad(alignment, ofs + _size);
  126. ofs = ofs + _size + pad;
  127. files.push_back(pf);
  128. f->close();
  129. memdelete(f);
  130. return OK;
  131. }
  132. Error PCKPacker::flush(bool p_verbose) {
  133. ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use.");
  134. int64_t file_base_ofs = file->get_position();
  135. file->store_64(0); // files base
  136. for (int i = 0; i < 16; i++) {
  137. file->store_32(0); // reserved
  138. }
  139. // write the index
  140. file->store_32(files.size());
  141. FileAccessEncrypted *fae = nullptr;
  142. FileAccess *fhead = file;
  143. if (enc_dir) {
  144. fae = memnew(FileAccessEncrypted);
  145. ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE);
  146. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  147. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  148. fhead = fae;
  149. }
  150. for (int i = 0; i < files.size(); i++) {
  151. int string_len = files[i].path.utf8().length();
  152. int pad = _get_pad(4, string_len);
  153. fhead->store_32(string_len + pad);
  154. fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len);
  155. for (int j = 0; j < pad; j++) {
  156. fhead->store_8(0);
  157. }
  158. fhead->store_64(files[i].ofs);
  159. fhead->store_64(files[i].size); // pay attention here, this is where file is
  160. fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file
  161. uint32_t flags = 0;
  162. if (files[i].encrypted) {
  163. flags |= PACK_FILE_ENCRYPTED;
  164. }
  165. fhead->store_32(flags);
  166. }
  167. if (fae) {
  168. fae->release();
  169. memdelete(fae);
  170. }
  171. int header_padding = _get_pad(alignment, file->get_position());
  172. for (int i = 0; i < header_padding; i++) {
  173. file->store_8(Math::rand() % 256);
  174. }
  175. int64_t file_base = file->get_position();
  176. file->seek(file_base_ofs);
  177. file->store_64(file_base); // update files base
  178. file->seek(file_base);
  179. const uint32_t buf_max = 65536;
  180. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  181. int count = 0;
  182. for (int i = 0; i < files.size(); i++) {
  183. FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
  184. uint64_t to_write = files[i].size;
  185. fae = nullptr;
  186. FileAccess *ftmp = file;
  187. if (files[i].encrypted) {
  188. fae = memnew(FileAccessEncrypted);
  189. ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE);
  190. Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  191. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  192. ftmp = fae;
  193. }
  194. while (to_write > 0) {
  195. int read = src->get_buffer(buf, MIN(to_write, buf_max));
  196. ftmp->store_buffer(buf, read);
  197. to_write -= read;
  198. }
  199. if (fae) {
  200. fae->release();
  201. memdelete(fae);
  202. }
  203. int pad = _get_pad(alignment, file->get_position());
  204. for (int j = 0; j < pad; j++) {
  205. file->store_8(Math::rand() % 256);
  206. }
  207. src->close();
  208. memdelete(src);
  209. count += 1;
  210. const int file_num = files.size();
  211. if (p_verbose && (file_num > 0)) {
  212. if (count % 100 == 0) {
  213. printf("%i/%i (%.2f)\r", count, file_num, float(count) / file_num * 100);
  214. fflush(stdout);
  215. }
  216. }
  217. }
  218. if (p_verbose) {
  219. printf("\n");
  220. }
  221. file->close();
  222. memdelete_arr(buf);
  223. return OK;
  224. }
  225. PCKPacker::~PCKPacker() {
  226. if (file != nullptr) {
  227. memdelete(file);
  228. }
  229. file = nullptr;
  230. }