2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2017-01-16 08:04:19 +01:00
|
|
|
/* pck_packer.cpp */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-08-02 03:10:38 +02:00
|
|
|
#include "pck_packer.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
#include "core/crypto/crypto_core.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/file_access.h"
|
2020-04-28 19:51:29 +02:00
|
|
|
#include "core/io/file_access_encrypted.h"
|
2019-11-30 17:22:22 +01:00
|
|
|
#include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/version.h"
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
static int _get_pad(int p_alignment, int p_n) {
|
|
|
|
int rest = p_n % p_alignment;
|
|
|
|
int pad = 0;
|
|
|
|
if (rest > 0) {
|
|
|
|
pad = p_alignment - rest;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
return pad;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
void PCKPacker::_bind_methods() {
|
2021-11-17 19:07:29 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(32), DEFVAL("0000000000000000000000000000000000000000000000000000000000000000"), DEFVAL(false));
|
2020-04-28 19:51:29 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false));
|
2019-12-07 01:05:11 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false));
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String &p_key, bool p_encrypt_directory) {
|
2020-12-15 13:04:21 +01:00
|
|
|
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).");
|
2021-11-17 19:07:29 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(p_alignment <= 0, ERR_CANT_CREATE, "Invalid alignment, must be greater then 0.");
|
2020-04-28 19:51:29 +02:00
|
|
|
|
|
|
|
String _key = p_key.to_lower();
|
|
|
|
key.resize(32);
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
int v = 0;
|
|
|
|
if (i * 2 < _key.length()) {
|
|
|
|
char32_t ct = _key[i * 2];
|
2022-02-04 09:32:20 +01:00
|
|
|
if (is_digit(ct)) {
|
2020-04-28 19:51:29 +02:00
|
|
|
ct = ct - '0';
|
|
|
|
} else if (ct >= 'a' && ct <= 'f') {
|
|
|
|
ct = 10 + ct - 'a';
|
|
|
|
}
|
|
|
|
v |= ct << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i * 2 + 1 < _key.length()) {
|
|
|
|
char32_t ct = _key[i * 2 + 1];
|
2022-02-04 09:32:20 +01:00
|
|
|
if (is_digit(ct)) {
|
2020-04-28 19:51:29 +02:00
|
|
|
ct = ct - '0';
|
|
|
|
} else if (ct >= 'a' && ct <= 'f') {
|
|
|
|
ct = 10 + ct - 'a';
|
|
|
|
}
|
|
|
|
v |= ct;
|
|
|
|
}
|
|
|
|
key.write[i] = v;
|
|
|
|
}
|
|
|
|
enc_dir = p_encrypt_directory;
|
|
|
|
|
2014-08-02 03:10:38 +02:00
|
|
|
file = FileAccess::open(p_file, FileAccess::WRITE);
|
2022-03-23 10:08:58 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(file.is_null(), ERR_CANT_CREATE, "Can't open file to write: " + String(p_file) + ".");
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
alignment = p_alignment;
|
|
|
|
|
2019-11-30 17:22:22 +01:00
|
|
|
file->store_32(PACK_HEADER_MAGIC);
|
|
|
|
file->store_32(PACK_FORMAT_VERSION);
|
|
|
|
file->store_32(VERSION_MAJOR);
|
|
|
|
file->store_32(VERSION_MINOR);
|
|
|
|
file->store_32(VERSION_PATCH);
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
uint32_t pack_flags = 0;
|
|
|
|
if (enc_dir) {
|
|
|
|
pack_flags |= PACK_DIR_ENCRYPTED;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2020-04-28 19:51:29 +02:00
|
|
|
file->store_32(pack_flags); // flags
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
files.clear();
|
2020-04-28 19:51:29 +02:00
|
|
|
ofs = 0;
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
return OK;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) {
|
2022-10-19 15:19:02 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccess> f = FileAccess::open(p_src, FileAccess::READ);
|
|
|
|
if (f.is_null()) {
|
2014-08-02 03:10:38 +02:00
|
|
|
return ERR_FILE_CANT_OPEN;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
File pf;
|
|
|
|
pf.path = p_file;
|
|
|
|
pf.src_path = p_src;
|
2020-04-28 19:51:29 +02:00
|
|
|
pf.ofs = ofs;
|
2021-05-25 08:58:49 +02:00
|
|
|
pf.size = f->get_length();
|
2020-04-28 19:51:29 +02:00
|
|
|
|
|
|
|
Vector<uint8_t> data = FileAccess::get_file_as_array(p_src);
|
|
|
|
{
|
|
|
|
unsigned char hash[16];
|
|
|
|
CryptoCore::md5(data.ptr(), data.size(), hash);
|
|
|
|
pf.md5.resize(16);
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
pf.md5.write[i] = hash[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pf.encrypted = p_encrypt;
|
|
|
|
|
|
|
|
uint64_t _size = pf.size;
|
|
|
|
if (p_encrypt) { // Add encryption overhead.
|
|
|
|
if (_size % 16) { // Pad to encryption block size.
|
|
|
|
_size += 16 - (_size % 16);
|
|
|
|
}
|
|
|
|
_size += 16; // hash
|
|
|
|
_size += 8; // data size
|
|
|
|
_size += 16; // iv
|
|
|
|
}
|
|
|
|
|
|
|
|
int pad = _get_pad(alignment, ofs + _size);
|
|
|
|
ofs = ofs + _size + pad;
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
files.push_back(pf);
|
|
|
|
|
|
|
|
return OK;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
Error PCKPacker::flush(bool p_verbose) {
|
2022-03-23 10:08:58 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use.");
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
int64_t file_base_ofs = file->get_position();
|
|
|
|
file->store_64(0); // files base
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
file->store_32(0); // reserved
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the index
|
2014-08-02 03:10:38 +02:00
|
|
|
file->store_32(files.size());
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccessEncrypted> fae;
|
|
|
|
Ref<FileAccess> fhead = file;
|
2020-04-28 19:51:29 +02:00
|
|
|
|
|
|
|
if (enc_dir) {
|
2022-03-23 10:08:58 +01:00
|
|
|
fae.instantiate();
|
|
|
|
ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
|
2020-04-28 19:51:29 +02:00
|
|
|
|
|
|
|
Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
|
|
|
|
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
|
|
|
|
|
|
|
|
fhead = fae;
|
|
|
|
}
|
|
|
|
|
2014-08-02 03:10:38 +02:00
|
|
|
for (int i = 0; i < files.size(); i++) {
|
2020-04-28 19:51:29 +02:00
|
|
|
int string_len = files[i].path.utf8().length();
|
|
|
|
int pad = _get_pad(4, string_len);
|
|
|
|
|
|
|
|
fhead->store_32(string_len + pad);
|
|
|
|
fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len);
|
|
|
|
for (int j = 0; j < pad; j++) {
|
|
|
|
fhead->store_8(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fhead->store_64(files[i].ofs);
|
|
|
|
fhead->store_64(files[i].size); // pay attention here, this is where file is
|
|
|
|
fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file
|
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (files[i].encrypted) {
|
|
|
|
flags |= PACK_FILE_ENCRYPTED;
|
|
|
|
}
|
|
|
|
fhead->store_32(flags);
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
if (fae.is_valid()) {
|
2022-04-12 10:15:02 +02:00
|
|
|
fhead.unref();
|
|
|
|
fae.unref();
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
int header_padding = _get_pad(alignment, file->get_position());
|
|
|
|
for (int i = 0; i < header_padding; i++) {
|
|
|
|
file->store_8(Math::rand() % 256);
|
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
int64_t file_base = file->get_position();
|
|
|
|
file->seek(file_base_ofs);
|
|
|
|
file->store_64(file_base); // update files base
|
|
|
|
file->seek(file_base);
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
const uint32_t buf_max = 65536;
|
|
|
|
uint8_t *buf = memnew_arr(uint8_t, buf_max);
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < files.size(); i++) {
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccess> src = FileAccess::open(files[i].src_path, FileAccess::READ);
|
2014-08-02 03:10:38 +02:00
|
|
|
uint64_t to_write = files[i].size;
|
2020-04-28 19:51:29 +02:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccess> ftmp = file;
|
2020-04-28 19:51:29 +02:00
|
|
|
if (files[i].encrypted) {
|
2022-03-23 10:08:58 +01:00
|
|
|
fae.instantiate();
|
|
|
|
ERR_FAIL_COND_V(fae.is_null(), ERR_CANT_CREATE);
|
2020-04-28 19:51:29 +02:00
|
|
|
|
|
|
|
Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
|
|
|
|
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
|
|
|
|
ftmp = fae;
|
|
|
|
}
|
|
|
|
|
2014-08-02 03:10:38 +02:00
|
|
|
while (to_write > 0) {
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max));
|
2020-04-28 19:51:29 +02:00
|
|
|
ftmp->store_buffer(buf, read);
|
2014-08-02 03:10:38 +02:00
|
|
|
to_write -= read;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
if (fae.is_valid()) {
|
2022-04-12 10:15:02 +02:00
|
|
|
ftmp.unref();
|
|
|
|
fae.unref();
|
2020-04-28 19:51:29 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
int pad = _get_pad(alignment, file->get_position());
|
|
|
|
for (int j = 0; j < pad; j++) {
|
|
|
|
file->store_8(Math::rand() % 256);
|
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
count += 1;
|
2020-04-28 19:51:29 +02:00
|
|
|
const int file_num = files.size();
|
|
|
|
if (p_verbose && (file_num > 0)) {
|
2022-02-25 02:32:20 +01:00
|
|
|
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));
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_verbose) {
|
2014-08-02 03:10:38 +02:00
|
|
|
printf("\n");
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-02 03:10:38 +02:00
|
|
|
|
2022-04-12 09:12:40 +02:00
|
|
|
file.unref();
|
2019-01-22 18:02:19 +01:00
|
|
|
memdelete_arr(buf);
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
return OK;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|