SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
VICE.cpp
Go to the documentation of this file.
1#include <vcryptpp/VICE.h>
2
3#include <cstring>
4
5#include <IceKey.h>
6
7using namespace vcryptpp;
8
9namespace {
10
11std::vector<std::byte> applyIce(std::span<const std::byte> data, std::string_view code, bool encrypt) {
12 IceKey ice;
13 ice.set(reinterpret_cast<const unsigned char*>(code.data()));
14
15 const auto* in = reinterpret_cast<const unsigned char*>(data.data());
16
17 std::vector<std::byte> outVec(data.size());
18 auto* out = reinterpret_cast<unsigned char*>(outVec.data());
19
20 auto bytesLeft = data.size();
21 while (bytesLeft >= IceKey::blockSize()) {
22 if (encrypt) {
23 ice.encrypt(in, out);
24 } else {
25 ice.decrypt(in, out);
26 }
27 bytesLeft -= IceKey::blockSize();
28 in += IceKey::blockSize();
29 out += IceKey::blockSize();
30 }
31 if (bytesLeft) {
32 std::memcpy(out, in, bytesLeft);
33 }
34
35 return outVec;
36}
37
38} // namespace
39
40std::vector<std::byte> VICE::encrypt(std::span<const std::byte> data, std::string_view code) {
41 return ::applyIce(data, code, true);
42}
43
44std::vector<std::byte> VICE::decrypt(std::span<const std::byte> data, std::string_view code) {
45 return ::applyIce(data, code, false);
46}
std::vector< std::byte > decrypt(std::span< const std::byte > data, std::string_view code=KnownCodes::DEFAULT)
Definition: VICE.cpp:44
std::vector< std::byte > encrypt(std::span< const std::byte > data, std::string_view code=KnownCodes::DEFAULT)
Definition: VICE.cpp:40
Definition: VFONT.h:10