From 34e945128f57b81e1d6355b00b04bd3c7cde6748 Mon Sep 17 00:00:00 2001 From: Eladash <18193363+elad335@users.noreply.github.com> Date: Wed, 14 Feb 2024 14:08:46 +0200 Subject: [PATCH] Crypto\EDATA: Optimize decrypt_block to avoid an allocation --- rpcs3/Crypto/unedat.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rpcs3/Crypto/unedat.cpp b/rpcs3/Crypto/unedat.cpp index d8a7a7076e..590037f4f0 100644 --- a/rpcs3/Crypto/unedat.cpp +++ b/rpcs3/Crypto/unedat.cpp @@ -157,7 +157,7 @@ u128 get_block_key(int block, NPD_HEADER *npd) // for out data, allocate a buffer the size of 'edat->block_size' // Also, set 'in file' to the beginning of the encrypted data, which may be offset if inside another file, but normally just reset to beginning of file // returns number of bytes written, -1 for error -s64 decrypt_block(const fs::file* in, u8* out, EDAT_HEADER *edat, NPD_HEADER *npd, u8* crypt_key, u32 block_num, u32 total_blocks, u64 size_left) +s64 decrypt_block(const fs::file* in, u8* out, EDAT_HEADER *edat, NPD_HEADER *npd, u8* crypt_key, u32 block_num, u32 total_blocks, u64 size_left, bool is_out_buffer_aligned = false) { // Get metadata info and setup buffers. const int metadata_section_size = ((edat->flags & EDAT_COMPRESSED_FLAG) != 0 || (edat->flags & EDAT_FLAG_0x20) != 0) ? 0x20 : 0x10; @@ -235,11 +235,11 @@ s64 decrypt_block(const fs::file* in, u8* out, EDAT_HEADER *edat, NPD_HEADER *np length = utils::align(pad_length, 0x10); // Setup buffers for decryption and read the data. - std::vector enc_data_buf(length == pad_length ? 0 : length); + std::vector enc_data_buf(is_out_buffer_aligned || length == pad_length ? 0 : length); std::vector dec_data_buf(length); // Try to use out buffer for file reads if no padding is needed instead of a new buffer - u8* enc_data = length == pad_length ? out : enc_data_buf.data(); + u8* enc_data = enc_data_buf.empty() ? out : enc_data_buf.data(); // Variable to avoid copies when possible u8* dec_data = dec_data_buf.data(); @@ -895,11 +895,11 @@ u64 EDATADecrypter::ReadData(u64 pos, u8* data, u64 size) u64 writeOffset = 0; - std::vector data_buf(edatHeader.block_size); + std::vector data_buf(edatHeader.block_size + 16); for (u32 i = starting_block; i < ending_block; i++) { - u64 res = decrypt_block(&edata_file, data_buf.data(), &edatHeader, &npdHeader, reinterpret_cast(&dec_key), i, total_blocks, edatHeader.file_size); + u64 res = decrypt_block(&edata_file, data_buf.data(), &edatHeader, &npdHeader, reinterpret_cast(&dec_key), i, total_blocks, edatHeader.file_size, true); if (res == umax) {