Load iw4x zones for iw4

This commit is contained in:
Jan
2021-04-01 20:29:42 +02:00
parent 590a108a1b
commit 1cc5be2f64
6 changed files with 99 additions and 8 deletions

View File

@ -0,0 +1,45 @@
#include "ProcessorIW4xDecryption.h"
#include <cassert>
ProcessorIW4xDecryption::ProcessorIW4xDecryption()
: m_last_byte(0u)
{
}
uint8_t ProcessorIW4xDecryption::RotateLeft(const uint8_t value, const unsigned count)
{
assert(count < sizeof(value) * 8);
return static_cast<uint8_t>(value << count | (value >> ((sizeof(value) * 8) - count)));
}
uint8_t ProcessorIW4xDecryption::RotateRight(uint8_t value, const unsigned count)
{
assert(count < sizeof(value) * 8);
return static_cast<uint8_t>(value >> count | (value << ((sizeof(value) * 8) - count)));
}
size_t ProcessorIW4xDecryption::Load(void* buffer, const size_t length)
{
const auto readLen = m_base_stream->Load(buffer, length);
auto* charBuffer = static_cast<uint8_t*>(buffer);
for(auto i = 0u; i < readLen; i++)
{
auto value = charBuffer[i];
value ^= m_last_byte;
value = RotateLeft(value, 4);
value ^= -1;
value = RotateRight(value, 6);
charBuffer[i] = value;
m_last_byte = value;
}
return readLen;
}
int64_t ProcessorIW4xDecryption::Pos()
{
return m_base_stream->Pos();
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Loading/StreamProcessor.h"
class ProcessorIW4xDecryption final : public StreamProcessor
{
uint8_t m_last_byte;
static uint8_t RotateLeft(uint8_t value, unsigned count);
static uint8_t RotateRight(uint8_t value, unsigned count);
public:
ProcessorIW4xDecryption();
size_t Load(void* buffer, size_t length) override;
int64_t Pos() override;
};

View File

@ -69,7 +69,7 @@ public:
throw InvalidCompressionException();
}
return m_stream.avail_out;
return length - m_stream.avail_out;
}
};