TWP: Move btea to common

This commit is contained in:
scemino 2024-02-18 09:59:36 +01:00 committed by Eugene Sandulenko
parent b8e0a37037
commit 2423ba3ed6
5 changed files with 74 additions and 56 deletions

View File

@ -19,9 +19,9 @@
*
*/
#include "twp/btea.h"
#include "common/btea.h"
namespace Twp {
namespace Common {
#define DELTA 0x9e3779b9
#define MX (((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z)))
@ -37,7 +37,7 @@ void BTEACrypto::decrypt(uint32 *v, int n, const uint32 *key) {
// This method comes from https://en.wikipedia.org/wiki/XXTEA
void BTEACrypto::btea(uint32 *v, int n, const uint32 *key) {
uint32 y, z, sum;
unsigned p, rounds, e;
unsigned int p, rounds, e;
if (n > 1) { /* Coding Part */
rounds = 6 + 52 / n;
sum = 0;
@ -69,4 +69,4 @@ void BTEACrypto::btea(uint32 *v, int n, const uint32 *key) {
} while (--rounds);
}
}
} // namespace Twp
} // namespace Common

View File

@ -19,22 +19,40 @@
*
*/
#ifndef TWP_BTEA_H
#define TWP_BTEA_H
#ifndef COMMON_BTEA_H
#define COMMON_BTEA_H
#include "common/system.h"
namespace Twp {
namespace Common {
/**
* Corrected Block TEA (aka XXTEA) class for ScummVM.
*
* In cryptography, Corrected Block TEA (often referred to as XXTEA)
* is a block cipher designed to correct weaknesses in the original Block TEA.
*/
class BTEACrypto {
public:
static void encrypt(uint32 *v, int n, const uint32 *k);
static void decrypt(uint32 *v, int n, const uint32 *k);
/**
* Encrypt data with a specified key.
* @param[in,out] data the data to encrypt
* @param[in] n the size of the data
* @param[in] key the key to use to encrypt the data
*/
static void encrypt(uint32 *data, int n, const uint32 *key);
/**
* Decrypt data encrypted before with btea with a specified key.
* @param[in,out] data the data to decrypt
* @param[in] n the size of the data
* @param[in] key the key to use to decrypt the data
*/
static void decrypt(uint32 *data, int n, const uint32 *key);
private:
static void btea(uint32 *v, int n, const uint32 *k);
static void btea(uint32 *v, int n, const uint32 *k);
};
} // End of namespace Twp
} // End of namespace Common
#endif // TWP_BTEA_H
#endif // COMMON_BTEA_H

View File

@ -2,6 +2,7 @@ MODULE := common
MODULE_OBJS := \
archive.o \
btea.o \
concatstream.o \
config-manager.o \
coroutines.o \

View File

@ -1,50 +1,49 @@
MODULE := engines/twp
MODULE_OBJS = \
twp.o \
console.o \
metaengine.o \
vm.o \
ggpack.o \
gfx.o \
resmanager.o \
spritesheet.o \
room.o \
lighting.o \
font.o \
sqgame.o \
syslib.o \
objlib.o \
genlib.o \
squtil.o \
thread.o \
rectf.o \
scenegraph.o \
object.o \
ids.o \
camera.o \
actorlib.o \
roomlib.o \
soundlib.o \
prefs.o \
tsv.o \
util.o \
motor.o \
yack.o \
dialog.o \
shaders.o \
hud.o \
lip.o \
callback.o \
graph.o \
walkboxnode.o \
actorswitcher.o \
enginedialogtarget.o \
audio.o \
savegame.o \
btea.o \
time.o \
callback.o \
camera.o \
console.o \
dialog.o \
dialogs.o \
enginedialogtarget.o \
font.o \
genlib.o \
gfx.o \
ggpack.o \
graph.o \
hud.o \
ids.o \
lighting.o \
lip.o \
metaengine.o \
motor.o \
object.o \
objlib.o \
prefs.o \
resmanager.o \
rectf.o \
room.o \
roomlib.o \
scenegraph.o \
shaders.o \
soundlib.o \
savegame.o \
spritesheet.o \
sqgame.o \
squtil.o \
syslib.o \
thread.o \
time.o \
tsv.o \
twp.o \
util.o \
vm.o \
walkboxnode.o \
yack.o \
squirrel/sqapi.o \
squirrel/sqbaselib.o \
squirrel/sqfuncstate.o \

View File

@ -19,12 +19,12 @@
*
*/
#include "common/btea.h"
#include "common/savefile.h"
#include "twp/ggpack.h"
#include "twp/savegame.h"
#include "twp/squtil.h"
#include "twp/btea.h"
#include "twp/time.h"
#include "common/savefile.h"
namespace Twp {
@ -424,7 +424,7 @@ bool SaveGameManager::loadGame(const SaveGame &savegame) {
bool SaveGameManager::getSaveGame(Common::SeekableReadStream *stream, SaveGame &savegame) {
Common::Array<byte> data(stream->size());
stream->read(data.data(), data.size());
BTEACrypto::decrypt((uint32 *)data.data(), data.size() / 4, savegameKey);
Common::BTEACrypto::decrypt((uint32 *)data.data(), data.size() / 4, savegameKey);
savegame.hashData = *(int32_t *)(&data[data.size() - 16]);
savegame.time = *(int32_t *)&data[data.size() - 12];
int32_t hashCheck = computeHash(data.data(), data.size() - 16);
@ -1006,7 +1006,7 @@ void SaveGameManager::saveGame(Common::WriteStream *ws) {
memset(&p[2], marker, 8);
// then encode data
BTEACrypto::encrypt((uint32 *)buffer.data(), buffer.size() / 4, savegameKey);
Common::BTEACrypto::encrypt((uint32 *)buffer.data(), buffer.size() / 4, savegameKey);
// and write data
ws->write(buffer.data(), buffer.size());