mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-01-24 17:03:05 +00:00
refactor: hide implementation details of zone loading processors
This commit is contained in:
@@ -2,44 +2,62 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
ProcessorIW4xDecryption::ProcessorIW4xDecryption()
|
||||
: m_last_byte(0u)
|
||||
namespace
|
||||
{
|
||||
}
|
||||
|
||||
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++)
|
||||
uint8_t RotateLeft(const uint8_t value, const unsigned count)
|
||||
{
|
||||
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;
|
||||
assert(count < sizeof(value) * 8);
|
||||
return static_cast<uint8_t>(value << count | (value >> ((sizeof(value) * 8) - count)));
|
||||
}
|
||||
|
||||
return readLen;
|
||||
}
|
||||
uint8_t RotateRight(const uint8_t value, const unsigned count)
|
||||
{
|
||||
assert(count < sizeof(value) * 8);
|
||||
return static_cast<uint8_t>(value >> count | (value << ((sizeof(value) * 8) - count)));
|
||||
}
|
||||
|
||||
int64_t ProcessorIW4xDecryption::Pos()
|
||||
class ProcessorIW4xDecryption final : public StreamProcessor
|
||||
{
|
||||
public:
|
||||
ProcessorIW4xDecryption()
|
||||
: m_last_byte(0u)
|
||||
{
|
||||
}
|
||||
|
||||
size_t Load(void* buffer, const size_t length) override
|
||||
{
|
||||
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 Pos() override
|
||||
{
|
||||
return m_base_stream->Pos();
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t m_last_byte;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace processor
|
||||
{
|
||||
return m_base_stream->Pos();
|
||||
}
|
||||
std::unique_ptr<StreamProcessor> CreateProcessorIW4xDecryption()
|
||||
{
|
||||
return std::make_unique<ProcessorIW4xDecryption>();
|
||||
}
|
||||
} // namespace processor
|
||||
|
||||
Reference in New Issue
Block a user