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 if (code.size() != IceKey::blockSize()) {
13 return {};
14 }
15
16 IceKey ice;
17 ice.set(reinterpret_cast<const unsigned char*>(code.data()));
18
19 const auto* in = reinterpret_cast<const unsigned char*>(data.data());
20
21 std::vector<std::byte> outVec(data.size());
22 auto* out = reinterpret_cast<unsigned char*>(outVec.data());
23
24 auto bytesLeft = data.size();
25 while (bytesLeft >= IceKey::blockSize()) {
26 if (encrypt) {
27 ice.encrypt(in, out);
28 } else {
29 ice.decrypt(in, out);
30 }
31 bytesLeft -= IceKey::blockSize();
32 in += IceKey::blockSize();
33 out += IceKey::blockSize();
34 }
35 if (bytesLeft) {
36 std::memcpy(out, in, bytesLeft);
37 }
38
39 return outVec;
40}
41
42} // namespace
43
44std::vector<std::byte> VICE::encrypt(std::span<const std::byte> data, std::string_view code) {
45 return ::applyIce(data, code, true);
46}
47
48std::vector<std::byte> VICE::decrypt(std::span<const std::byte> data, std::string_view code) {
49 return ::applyIce(data, code, false);
50}
std::vector< std::byte > decrypt(std::span< const std::byte > data, std::string_view code=KnownCodes::DEFAULT)
Definition: VICE.cpp:48
std::vector< std::byte > encrypt(std::span< const std::byte > data, std::string_view code=KnownCodes::DEFAULT)
Definition: VICE.cpp:44
Definition: VFONT.h:10