SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Toggle main menu visibility
Loading...
Searching...
No Matches
VFONT.cpp
Go to the documentation of this file.
1
#include <
vcryptpp/VFONT.h
>
2
3
#include <limits>
4
#include <random>
5
#include <string_view>
6
7
#include <BufferStream.h>
8
9
using namespace
vcryptpp
;
10
11
std::vector<std::byte>
VFONT::encrypt
(std::span<const std::byte> data, uint8_t saltLength) {
12
std::vector<std::byte> out;
13
BufferStream stream{out};
14
15
static
std::random_device random_device{};
16
static
std::mt19937 generator{random_device()};
17
std::uniform_int_distribution<> distribution{0, (std::numeric_limits<uint8_t>::max())};
18
19
std::vector<std::byte> salt;
20
salt.resize(saltLength);
21
22
uint8_t magic =
MAGIC
;
23
for
(
int
i = 0; i < saltLength; i++) {
24
salt[i] =
static_cast<
std::byte
>
(distribution(generator));
25
magic ^=
static_cast<
uint8_t
>
(salt[i]) +
MAGIC
;
26
}
27
28
for
(
auto
byte
: data) {
29
const
uint8_t encrypted =
static_cast<
uint8_t
>
(byte) ^ magic;
30
stream << encrypted;
31
magic = encrypted +
MAGIC
;
32
}
33
stream
34
.write(salt)
35
.write<uint8_t>(saltLength + 1)
36
.write(
SIGNATURE
,
false
);
37
38
out.resize(stream.size());
39
return
out;
40
}
41
42
std::vector<std::byte>
VFONT::decrypt
(std::span<const std::byte> data) {
43
BufferStreamReadOnly reader{data};
44
45
if
(reader.seek(
SIGNATURE
.length(), std::ios::end).read_string(
SIGNATURE
.length()) !=
SIGNATURE
) {
46
return
{};
47
}
48
reader.seek(
SIGNATURE
.length() +
sizeof
(uint8_t), std::ios::end);
49
50
const
auto
bytes = reader.read<uint8_t>();
51
52
std::vector<std::byte> out;
53
out.resize(reader.size() -
SIGNATURE
.length() - bytes);
54
55
reader.seek(-
static_cast<
int
>
(bytes), std::ios::cur);
56
uint8_t magic =
MAGIC
;
57
for
(
int
i = 0; i < bytes - 1; i++) {
58
magic ^= reader.read<uint8_t>() +
MAGIC
;
59
}
60
61
reader.seek(0);
62
for
(
auto
& outByte : out) {
63
const
auto
byte
= reader.read<uint8_t>();
64
outByte =
static_cast<
std::byte
>
(
byte
^ magic);
65
magic =
byte
+
MAGIC
;
66
}
67
68
return
out;
69
}
VFONT.h
vcryptpp::VFONT::MAGIC
constexpr uint8_t MAGIC
Definition
VFONT.h:14
vcryptpp::VFONT::SIGNATURE
constexpr std::string_view SIGNATURE
Definition
VFONT.h:12
vcryptpp::VFONT::encrypt
std::vector< std::byte > encrypt(std::span< const std::byte > data, uint8_t saltLength=2)
Definition
VFONT.cpp:11
vcryptpp::VFONT::decrypt
std::vector< std::byte > decrypt(std::span< const std::byte > data)
Definition
VFONT.cpp:42
vcryptpp
Definition
VFONT.h:10
src
vcryptpp
VFONT.cpp
Generated on
for SourcePP by
1.17.0