| 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 | |
| 31 | #include "pck_packer.h" |
| 32 | |
| 33 | #include "core/crypto/crypto_core.h" |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "core/io/file_access_encrypted.h" |
| 36 | #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION |
| 37 | #include "core/version.h" |
| 38 | |
| 39 | static int _get_pad(int p_alignment, int p_n) { |
| 40 | int rest = p_n % p_alignment; |
| 41 | int pad = 0; |
| 42 | if (rest > 0) { |
| 43 | pad = p_alignment - rest; |
| 44 | } |
| 45 | |
| 46 | return pad; |
| 47 | } |
| 48 | |
| 49 | void PCKPacker::_bind_methods() { |
| 50 | ClassDB::bind_method(D_METHOD("pck_start", "pck_path", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(32), DEFVAL("0000000000000000000000000000000000000000000000000000000000000000"), DEFVAL(false)); |
| 51 | ClassDB::bind_method(D_METHOD("add_file", "target_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false)); |
| 52 | ClassDB::bind_method(D_METHOD("add_file_removal", "target_path"), &PCKPacker::add_file_removal); |
| 53 | ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false)); |
| 54 | } |
| 55 | |
| 56 | Error PCKPacker::pck_start(const String &p_pck_path, int p_alignment, const String &p_key, bool p_encrypt_directory) { |
| 57 | 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)."); |
| 58 | ERR_FAIL_COND_V_MSG(p_alignment <= 0, ERR_CANT_CREATE, "Invalid alignment, must be greater then 0."); |
| 59 | |
| 60 | String _key = p_key.to_lower(); |
| 61 | key.resize(32); |
| 62 | for (int i = 0; i < 32; i++) { |
| 63 | int v = 0; |
| 64 | if (i * 2 < _key.length()) { |
| 65 | char32_t ct = _key[i * 2]; |
| 66 | if (is_digit(ct)) { |
| 67 | ct = ct - '0'; |
| 68 | } else if (ct >= 'a' && ct <= 'f') { |
| 69 | ct = 10 + ct - 'a'; |
| 70 | } |
| 71 | v |= ct << 4; |
| 72 | } |
| 73 | |
| 74 | if (i * 2 + 1 < _key.length()) { |
| 75 | char32_t ct = _key[i * 2 + 1]; |
| 76 | if (is_digit(ct)) { |
| 77 | ct = ct - '0'; |
| 78 | } else if (ct >= 'a' && ct <= 'f') { |
| 79 | ct = 10 + ct - 'a'; |
| 80 | } |
| 81 | v |= ct; |
| 82 | } |
| 83 | key.write[i] = v; |
| 84 | } |
| 85 | enc_dir = p_encrypt_directory; |
| 86 | |
| 87 | file = FileAccess::open(p_pck_path, FileAccess::WRITE); |
| 88 | ERR_FAIL_COND_V_MSG(file.is_null(), ERR_CANT_CREATE, vformat("Can't open file to write: '%s'.", String(p_pck_path))); |
| 89 | |
| 90 | alignment = p_alignment; |
| 91 | |
| 92 | file->store_32(PACK_HEADER_MAGIC); |
| 93 | file->store_32(PACK_FORMAT_VERSION); |
| 94 | file->store_32(GODOT_VERSION_MAJOR); |
| 95 | file->store_32(GODOT_VERSION_MINOR); |
| 96 | file->store_32(GODOT_VERSION_PATCH); |
| 97 | |
| 98 | uint32_t pack_flags = PACK_REL_FILEBASE; |
| 99 | if (enc_dir) { |
| 100 | pack_flags |= PACK_DIR_ENCRYPTED; |
| 101 | } |
| 102 | file->store_32(pack_flags); // flags |
| 103 | |
| 104 | file_base_ofs = file->get_position(); |
| 105 | file->store_64(0); // Files base. |
| 106 | |
| 107 | dir_base_ofs = file->get_position(); |
| 108 | file->store_64(0); // Directory offset. |
| 109 | |
| 110 | for (int i = 0; i < 16; i++) { |
| 111 | file->store_32(0); // Reserved. |
| 112 | } |
| 113 | |
| 114 | // Align for first file. |
| 115 | int pad = _get_pad(alignment, file->get_position()); |
| 116 | for (int i = 0; i < pad; i++) { |
| 117 | file->store_8(0); |
| 118 | } |
| 119 | |
| 120 | file_base = file->get_position(); |
| 121 | file->seek(file_base_ofs); |
| 122 | file->store_64(file_base); // Update files base. |
| 123 | file->seek(file_base); |
| 124 | |
| 125 | files.clear(); |
| 126 | |
| 127 | return OK; |
| 128 | } |
| 129 | |
| 130 | Error PCKPacker::add_file_removal(const String &p_target_path) { |
| 131 | ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use."); |
| 132 | |
| 133 | File pf; |
| 134 | // Simplify path here and on every 'files' access so that paths that have extra '/' |
| 135 | // symbols or 'res://' in them still match the MD5 hash for the saved path. |
| 136 | pf.path = p_target_path.simplify_path().trim_prefix("res://"); |
| 137 | pf.ofs = file->get_position(); |
| 138 | pf.size = 0; |
| 139 | pf.removal = true; |
| 140 | |
| 141 | pf.md5.resize_initialized(16); |
| 142 | |
| 143 | files.push_back(pf); |
| 144 | |
| 145 | return OK; |
| 146 | } |
| 147 | |
| 148 | Error PCKPacker::add_file(const String &p_target_path, const String &p_source_path, bool p_encrypt) { |
| 149 | ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use."); |
| 150 | |
| 151 | Ref<FileAccess> f = FileAccess::open(p_source_path, FileAccess::READ); |
| 152 | if (f.is_null()) { |
| 153 | return ERR_FILE_CANT_OPEN; |
| 154 | } |
| 155 | |
| 156 | File pf; |
| 157 | // Simplify path here and on every 'files' access so that paths that have extra '/' |
| 158 | // symbols or 'res://' in them still match the MD5 hash for the saved path. |
| 159 | pf.path = p_target_path.simplify_path().trim_prefix("res://"); |
| 160 | pf.src_path = p_source_path; |
| 161 | pf.ofs = file->get_position(); |
| 162 | pf.size = f->get_length(); |
| 163 | |
| 164 | Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_source_path); |
| 165 | { |
| 166 | unsigned char hash[16]; |
| 167 | CryptoCore::md5(data.ptr(), data.size(), hash); |
| 168 | pf.md5.resize(16); |
| 169 | for (int i = 0; i < 16; i++) { |
| 170 | pf.md5.write[i] = hash[i]; |
| 171 | } |
| 172 | } |
| 173 | pf.encrypted = p_encrypt; |
| 174 | |
| 175 | Ref<FileAccess> ftmp = file; |
| 176 | |
| 177 | Ref<FileAccessEncrypted> fae; |
| 178 | if (p_encrypt) { |
| 179 | fae.instantiate(); |
| 180 | ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE); |
| 181 | |
| 182 | Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false); |
| 183 | ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); |
| 184 | ftmp = fae; |
| 185 | } |
| 186 | |
| 187 | ftmp->store_buffer(data); |
| 188 | |
| 189 | if (fae.is_valid()) { |
| 190 | ftmp.unref(); |
| 191 | fae.unref(); |
| 192 | } |
| 193 | |
| 194 | int pad = _get_pad(alignment, file->get_position()); |
| 195 | for (int j = 0; j < pad; j++) { |
| 196 | file->store_8(0); |
| 197 | } |
| 198 | |
| 199 | files.push_back(pf); |
| 200 | |
| 201 | return OK; |
| 202 | } |
| 203 | |
| 204 | Error PCKPacker::flush(bool p_verbose) { |
| 205 | ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use."); |
| 206 | |
| 207 | int dir_padding = _get_pad(alignment, file->get_position()); |
| 208 | for (int i = 0; i < dir_padding; i++) { |
| 209 | file->store_8(0); |
| 210 | } |
| 211 | |
| 212 | // Write directory. |
| 213 | uint64_t dir_offset = file->get_position(); |
| 214 | file->seek(dir_base_ofs); |
| 215 | file->store_64(dir_offset); |
| 216 | file->seek(dir_offset); |
| 217 | |
| 218 | file->store_32(uint32_t(files.size())); |
| 219 | |
| 220 | Ref<FileAccessEncrypted> fae; |
| 221 | Ref<FileAccess> fhead = file; |
| 222 | |
| 223 | if (enc_dir) { |
| 224 | fae.instantiate(); |
| 225 | ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE); |
| 226 | |
| 227 | Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false); |
| 228 | ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); |
| 229 | |
| 230 | fhead = fae; |
| 231 | } |
| 232 | |
| 233 | const int file_num = files.size(); |
| 234 | for (int i = 0; i < file_num; i++) { |
| 235 | CharString utf8_string = files[i].path.utf8(); |
| 236 | int string_len = utf8_string.length(); |
| 237 | int pad = _get_pad(4, string_len); |
| 238 | |
| 239 | fhead->store_32(uint32_t(string_len + pad)); |
| 240 | fhead->store_buffer((const uint8_t *)utf8_string.get_data(), string_len); |
| 241 | for (int j = 0; j < pad; j++) { |
| 242 | fhead->store_8(0); |
| 243 | } |
| 244 | |
| 245 | fhead->store_64(files[i].ofs - file_base); |
| 246 | fhead->store_64(files[i].size); |
| 247 | fhead->store_buffer(files[i].md5.ptr(), 16); |
| 248 | |
| 249 | uint32_t flags = 0; |
| 250 | if (files[i].encrypted) { |
| 251 | flags |= PACK_FILE_ENCRYPTED; |
| 252 | } |
| 253 | if (files[i].removal) { |
| 254 | flags |= PACK_FILE_REMOVAL; |
| 255 | } |
| 256 | fhead->store_32(flags); |
| 257 | |
| 258 | if (p_verbose) { |
| 259 | print_line(vformat("[%d/%d - %d%%] PCKPacker flush: %s -> %s", i, file_num, float(i) / file_num * 100, files[i].src_path, files[i].path)); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (fae.is_valid()) { |
| 264 | fhead.unref(); |
| 265 | fae.unref(); |
| 266 | } |
| 267 | |
| 268 | file.unref(); |
| 269 | return OK; |
| 270 | } |
| 271 | |
| 272 | PCKPacker::~PCKPacker() { |
| 273 | if (file.is_valid()) { |
| 274 | flush(); |
| 275 | } |
| 276 | } |