Fix some warnings

This commit is contained in:
Eladash 2023-12-18 21:56:52 +02:00 committed by Elad Ashkenazi
parent d1527710f9
commit 90b6f5613e
5 changed files with 34 additions and 23 deletions

View File

@ -509,7 +509,7 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs:
// Setup signature hash.
if ((edat->flags & EDAT_FLAG_0x20) != 0) //Sony failed again, they used buffer from 0x100 with half size of real metadata.
{
int metadata_buf_size = block_num * 0x10;
const usz metadata_buf_size = block_num * 0x10;
std::unique_ptr<u8[]> metadata_buf(new u8[metadata_buf_size]);
f->seek(file_offset + metadata_offset);
f->read(metadata_buf.get(), metadata_buf_size);
@ -941,7 +941,7 @@ bool EDATADecrypter::ReadHeader()
}*/
file_size = edatHeader.file_size;
total_blocks = utils::aligned_div(edatHeader.file_size, edatHeader.block_size);
total_blocks = ::narrow<u32>(utils::aligned_div(edatHeader.file_size, edatHeader.block_size));
// Try decrypting the first block instead
u8 data_sample[1];

View File

@ -66,7 +66,7 @@ void hex_to_bytes(unsigned char* data, const char* hex_str, unsigned int str_len
// Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC).
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, int len)
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len)
{
aes_context ctx;
aes_setkey_dec(&ctx, key, 128);
@ -76,7 +76,7 @@ void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in,
memset(iv, 0, 0x10);
}
void aescbc128_encrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, int len)
void aescbc128_encrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len)
{
aes_context ctx;
aes_setkey_enc(&ctx, key, 128);
@ -93,7 +93,7 @@ void aesecb128_encrypt(unsigned char *key, unsigned char *in, unsigned char *out
aes_crypt_ecb(&ctx, AES_ENCRYPT, in, out);
}
bool hmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash, int hash_len)
bool hmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash, usz hash_len)
{
const std::unique_ptr<u8[]> out(new u8[key_len]);
@ -102,12 +102,12 @@ bool hmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, int i
return std::memcmp(out.get(), hash, hash_len) == 0;
}
void hmac_hash_forge(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash)
void hmac_hash_forge(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash)
{
sha1_hmac(key, key_len, in, in_len, hash);
}
bool cmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash, int hash_len)
bool cmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash, usz hash_len)
{
const std::unique_ptr<u8[]> out(new u8[key_len]);
@ -118,7 +118,7 @@ bool cmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, int i
return std::memcmp(out.get(), hash, hash_len) == 0;
}
void cmac_hash_forge(unsigned char *key, int /*key_len*/, unsigned char *in, int in_len, unsigned char *hash)
void cmac_hash_forge(unsigned char *key, int /*key_len*/, unsigned char *in, usz in_len, unsigned char *hash)
{
aes_context ctx;
aes_setkey_enc(&ctx, key, 128);

View File

@ -20,13 +20,13 @@ u64 hex_to_u64(const char* hex_str);
void hex_to_bytes(unsigned char *data, const char *hex_str, unsigned int str_length);
// Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC).
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, int len);
void aescbc128_encrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, int len);
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len);
void aescbc128_encrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len);
void aesecb128_encrypt(unsigned char *key, unsigned char *in, unsigned char *out);
bool hmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash, int hash_len);
void hmac_hash_forge(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash);
bool cmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash, int hash_len);
void cmac_hash_forge(unsigned char *key, int key_len, unsigned char *in, int in_len, unsigned char *hash);
bool hmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash, usz hash_len);
void hmac_hash_forge(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash);
bool cmac_hash_compare(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash, usz hash_len);
void cmac_hash_forge(unsigned char *key, int key_len, unsigned char *in, usz in_len, unsigned char *hash);
void mbedtls_zeroize(void *v, size_t n);
// SC passphrase crypto

View File

@ -370,7 +370,7 @@ static void ppu_initialize_modules(ppu_linkage_info* link, utils::serial* ar = n
auto& variable = _module->variables;
for (u32 i = 0, end = ar.pop<usz>(); i < end; i++)
for (usz i = 0, end = ar.pop<usz>(); i < end; i++)
{
auto* ptr = &::at32(variable, ar.pop<u32>());
ptr->addr = ar.pop<u32>();
@ -973,7 +973,7 @@ void ppu_manual_load_imports_exports(u32 imports_start, u32 imports_size, u32 ex
auto& link = g_fxo->get<ppu_linkage_info>();
ppu_module vm_all_fake_module{};
vm_all_fake_module.segs.emplace_back(ppu_segment{0x10000, -0x10000u, 1 /*LOAD*/, 0, -0x1000u, vm::base(0x10000)});
vm_all_fake_module.segs.emplace_back(ppu_segment{0x10000, 0 - 0x10000u, 1 /*LOAD*/, 0, 0 - 0x1000u, vm::base(0x10000)});
vm_all_fake_module.addr_to_seg_index.emplace(0x10000, 0);
ppu_load_exports(vm_all_fake_module, &link, exports_start, exports_start + exports_size, false, &loaded_flags);
@ -1084,13 +1084,13 @@ static void ppu_check_patch_spu_images(const ppu_module& mod, const ppu_segment&
auto find_first_of_multiple = [](std::string_view data, std::initializer_list<std::string_view> values, usz index)
{
usz pos = umax;
u32 pos = static_cast<u32>(data.size());
for (std::string_view value : values)
{
if (usz pos0 = data.substr(index, pos - index).find(value); pos0 != umax && pos0 + index < pos)
{
pos = pos0 + index;
pos = static_cast<u32>(pos0 + index);
}
}
@ -1102,9 +1102,9 @@ static void ppu_check_patch_spu_images(const ppu_module& mod, const ppu_segment&
// Search for [stqd lr,0x10(sp)] instruction or ELF file signature, whichever comes first
const std::initializer_list<std::string_view> prefixes = {"\177ELF"sv, "\x24\0\x40\x80"sv};
usz prev_bound = 0;
u32 prev_bound = 0;
for (usz i = find_first_of_multiple(seg_view, prefixes, 0); i < seg.size; i = find_first_of_multiple(seg_view, prefixes, utils::align<u32>(i + 1, 4)))
for (u32 i = find_first_of_multiple(seg_view, prefixes, 0); i < seg.size; i = find_first_of_multiple(seg_view, prefixes, utils::align<u32>(i + 1, 4)))
{
const auto elf_header = ensure(mod.get_ptr<u8>(seg.addr + i));
@ -1358,7 +1358,7 @@ static void ppu_check_patch_spu_images(const ppu_module& mod, const ppu_segment&
ppu_loader.success("SPU executable hash: %s (<- %u)%s", hash, applied.size(), dump);
}
i += obj.highest_offset - 4;
i += ::narrow<u32>(obj.highest_offset) - 4;
prev_bound = i + 4;
}
}
@ -1424,7 +1424,7 @@ struct prx_names_table
// Doesn't support addresses above 256MB because it wastes memory and is very unlikely (if somehow does occur increase it)
const u32 max0 = (seg.addr + seg.size - 1) >> 16;
const u32 max = std::min<u32>(std::size(lut), max0);
const u32 max = std::min<u32>(::size32(lut), max0);
if (max0 > max)
{

View File

@ -1045,7 +1045,18 @@ template <typename CT> requires requires (const CT& x) { std::size(x); }
const char* file = __builtin_FILE(),
const char* func = __builtin_FUNCTION())
{
return narrow<u32>(std::size(container), line, col, file, func);
// TODO: Supoort std::array
constexpr bool is_const = std::is_array_v<std::remove_cvref_t<CT>>;
if constexpr (is_const)
{
constexpr usz Size = sizeof(container) / sizeof(container[0]);
return std::conditional_t<is_const, u32, usz>{Size};
}
else
{
return narrow<u32>(std::size(container), line, col, file, func);
}
}
template <typename CT, typename T> requires requires (CT&& x) { std::size(x); std::data(x); } || requires (CT&& x) { std::size(x); x.front(); }