clean up big-endian encryption

This commit is contained in:
Martin Michelsen
2022-08-24 00:48:49 -07:00
parent e808a7b6a3
commit 77cea58fc5
3 changed files with 31 additions and 13 deletions
+22 -4
View File
@@ -81,7 +81,8 @@ uint32_t PSOV2Encryption::next(bool advance) {
return ret;
}
void PSOV2Encryption::encrypt(void* vdata, size_t size, bool advance) {
template <typename LongT>
void PSOV2Encryption::encrypt_t(void* vdata, size_t size, bool advance) {
if (size & 3) {
throw invalid_argument("size must be a multiple of 4");
}
@@ -90,12 +91,20 @@ void PSOV2Encryption::encrypt(void* vdata, size_t size, bool advance) {
}
size >>= 2;
le_uint32_t* data = reinterpret_cast<le_uint32_t*>(vdata);
LongT* data = reinterpret_cast<LongT*>(vdata);
for (size_t x = 0; x < size; x++) {
data[x] ^= this->next(advance);
}
}
void PSOV2Encryption::encrypt(void* vdata, size_t size, bool advance) {
this->encrypt_t<le_uint32_t>(vdata, size, advance);
}
void PSOV2Encryption::encrypt_big_endian(void* vdata, size_t size, bool advance) {
this->encrypt_t<be_uint32_t>(vdata, size, advance);
}
void PSOV3Encryption::update_stream() {
@@ -158,7 +167,8 @@ PSOV3Encryption::PSOV3Encryption(uint32_t seed) : offset(0) {
}
}
void PSOV3Encryption::encrypt(void* vdata, size_t size, bool advance) {
template <typename LongT>
void PSOV3Encryption::encrypt_t(void* vdata, size_t size, bool advance) {
if (size & 3) {
throw invalid_argument("size must be a multiple of 4");
}
@@ -167,12 +177,20 @@ void PSOV3Encryption::encrypt(void* vdata, size_t size, bool advance) {
}
size >>= 2;
le_uint32_t* data = reinterpret_cast<le_uint32_t*>(vdata);
LongT* data = reinterpret_cast<LongT*>(vdata);
for (size_t x = 0; x < size; x++) {
data[x] ^= this->next(advance);
}
}
void PSOV3Encryption::encrypt(void* vdata, size_t size, bool advance) {
this->encrypt_t<le_uint32_t>(vdata, size, advance);
}
void PSOV3Encryption::encrypt_big_endian(void* vdata, size_t size, bool advance) {
this->encrypt_t<be_uint32_t>(vdata, size, advance);
}
void PSOBBEncryption::decrypt(void* vdata, size_t size, bool advance) {