Merge pull request #19014 from hrydgard/save-data-cleanup

Save data code cleanup
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2024-04-07 14:17:40 +02:00
commit f54ab0c22c
6 files changed
+26 -25

No files matched your search

+10 -14
View File
@@ -145,7 +145,7 @@ namespace
int align16(int address)
{
return ((address + 0xF) >> 4) << 4;
return (address + 15) & ~15;
}
int GetSDKMainVersion(int sdkVersion)
@@ -537,10 +537,8 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD
delete[] updatedList;
// Init param with 0. This will be used to detect crypted save or not on loading
u8 *tmpData = new u8[128];
memset(tmpData, 0, 128);
sfoFile->SetValue("SAVEDATA_PARAMS", tmpData, 128, 128);
delete[] tmpData;
u8 zeroes[128]{};
sfoFile->SetValue("SAVEDATA_PARAMS", zeroes, 128, 128);
u8 *sfoData;
size_t sfoSize;
@@ -583,16 +581,14 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD
// copy back save name in request
strncpy(param->saveName, saveDirName.c_str(), 20);
if (fileName.empty()) {
delete[] cryptedData;
} else {
if (!fileName.empty()) {
if (!WritePSPFile(filePath, data_, saveSize)) {
ERROR_LOG(SCEUTILITY, "Error writing file %s", filePath.c_str());
delete[] cryptedData;
return SCE_UTILITY_SAVEDATA_ERROR_SAVE_MS_NOSPACE;
}
delete[] cryptedData;
}
delete[] cryptedData;
}
@@ -1010,6 +1006,7 @@ int SavedataParam::DecryptSave(unsigned int mode, unsigned char *data, int *data
return 0;
}
// Requires sfoData to be padded with zeroes to the next 16-byte boundary (due to BuildHash)
int SavedataParam::UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode)
{
int alignedLen = align16(sfoSize);
@@ -1059,19 +1056,18 @@ int SavedataParam::UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset,
return 0;
}
int SavedataParam::BuildHash(unsigned char *output,
unsigned char *data,
// Requires sfoData to be padded with zeroes to the next 16-byte boundary.
int SavedataParam::BuildHash(uint8_t *output,
const uint8_t *data,
unsigned int len,
unsigned int alignedLen,
int mode,
unsigned char *cryptkey)
{
const uint8_t *cryptkey) {
pspChnnlsvContext1 ctx1;
/* Set up buffers */
memset(&ctx1, 0, sizeof(pspChnnlsvContext1));
memset(output, 0, 0x10);
memset(data + len, 0, alignedLen - len);
/* Perform the magic */
if (sceSdSetIndex_(ctx1, mode & 0xFF) < 0)
+5 -1
View File
@@ -21,6 +21,8 @@
#include <mutex>
#include <set>
#include <unordered_map>
#include <cstdint>
#include "Common/CommonTypes.h"
#include "Core/ELF/ParamSFO.h"
#include "Core/MemMap.h"
@@ -375,7 +377,9 @@ private:
int DecryptSave(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *cryptkey, const u8 *expectedHash);
int EncryptData(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *hash, unsigned char *cryptkey);
int UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode);
int BuildHash(unsigned char *output, unsigned char *data, unsigned int len, unsigned int alignedLen, int mode, unsigned char *cryptkey);
// data must be zero-padded from len to alignedLen!
int BuildHash(uint8_t *output, const uint8_t *data, unsigned int len, unsigned int alignedLen, int mode, const uint8_t *cryptkey);
int DetermineCryptMode(const SceUtilitySavedataParam *param) const;
std::vector<SaveSFOFileListEntry> GetSFOEntries(const std::string &dirPath);
+2 -2
View File
@@ -195,7 +195,7 @@ bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) {
return true;
}
int ParamSFOData::GetDataOffset(const u8 *paramsfo, const std::string &dataName) {
int ParamSFOData::GetDataOffset(const u8 *paramsfo, const char *dataName) {
const Header *header = (const Header *)paramsfo;
if (header->magic != 0x46535000)
return -1;
@@ -210,7 +210,7 @@ int ParamSFOData::GetDataOffset(const u8 *paramsfo, const std::string &dataName)
for (u32 i = 0; i < header->index_table_entries; i++)
{
const char *key = (const char *)(key_start + indexTables[i].key_table_offset);
if (!strcmp(key, dataName.c_str()))
if (!strcmp(key, dataName))
{
return data_start + indexTables[i].data_table_offset;
}
+2 -1
View File
@@ -55,7 +55,8 @@ public:
}
}
int GetDataOffset(const u8 *paramsfo, const std::string &dataName);
// If not found, returns a negative value.
int GetDataOffset(const u8 *paramsfo, const char *dataName);
void Clear();
+4 -4
View File
@@ -134,7 +134,7 @@ static int sub_15B0(u8* data, int alignedLen, u8* buf, int val)
return 0;
}
static int sub_0000(u8* data_out, u8* data, int alignedLen, u8* data2, int& data3, int mode)
static int sub_0000(u8* data_out, u8* data, int alignedLen, const u8* data2, int& data3, int mode)
{
memcpy(data_out+20, data2, 16);
// Mode 1:2 is 83, 3:4 is 87, 5:6 is 100
@@ -222,7 +222,7 @@ static int sceSdGetLastIndex(u32 addressCtx, u32 addressHash, u32 addressKey) {
return hleLogSuccessI(SCEMISC, sceSdGetLastIndex_(*ctx, hash, Memory::GetPointerWrite(addressKey)));
}
int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, u8* in_key)
int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, const u8* in_key)
{
if(ctx.keyLength >= 17)
return -1026;
@@ -348,7 +348,7 @@ static int sceSdRemoveValue(u32 addressCtx, u32 addressData, int length) {
return hleLogSuccessI(SCEMISC, sceSdRemoveValue_(*ctx, Memory::GetPointerWrite(addressData), length));
}
int sceSdRemoveValue_(pspChnnlsvContext1& ctx, u8* data, int length)
int sceSdRemoveValue_(pspChnnlsvContext1& ctx, const u8* data, int length)
{
if(ctx.keyLength >= 17)
return -1026;
@@ -400,7 +400,7 @@ static int sceSdCreateList(u32 ctx2Addr, int mode, int unkwn, u32 dataAddr, u32
return hleLogSuccessI(SCEMISC, sceSdCreateList_(*ctx2, mode, unkwn, data, cryptkey));
}
int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, u8* cryptkey)
int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, const u8* cryptkey)
{
ctx2.mode = mode;
ctx2.unkn = 1;
+3 -3
View File
@@ -35,10 +35,10 @@ typedef struct _pspChnnlsvContext2 {
} pspChnnlsvContext2;
int sceSdSetIndex_(pspChnnlsvContext1& ctx, int value);
int sceSdRemoveValue_(pspChnnlsvContext1& ctx, u8* data, int length);
int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, u8* cryptkey);
int sceSdRemoveValue_(pspChnnlsvContext1& ctx, const u8* data, int length);
int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, const u8* cryptkey);
int sceSdSetMember_(pspChnnlsvContext2& ctx, u8* data, int alignedLen);
int sceSdCleanList_(pspChnnlsvContext2& ctx);
int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, u8* in_key);
int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, const u8* in_key);
void Register_sceChnnlsv();