Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Cache.cpp
Go to the documentation of this file.
1#pragma comment(lib, "libcrypto.lib")
2
3#include "Cache.h"
4
5#include <unordered_map>
6#include <string>
7#include <cstdint>
8#include <filesystem>
9#include <openssl/evp.h>
10
11namespace Cache
12{
14 {
15 std::ifstream file(filename, std::ios::binary);
16 if (!file.is_open()) {
17 Log::GetLog()->error("Error opening file for SHA-256 calculation: " + filename.string());
18 return "";
19 }
20
21 const auto fileSize = std::filesystem::file_size(filename);
22 std::vector<char> buffer(fileSize);
23
24 if (!file.read(buffer.data(), fileSize)) {
25 Log::GetLog()->error("Error reading file for SHA-256 calculation: " + filename.string());
26 return "";
27 }
28
29 std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> mdctx(EVP_MD_CTX_new(), &EVP_MD_CTX_free);
30 if (mdctx == nullptr) {
31 Log::GetLog()->error("Error creating EVP_MD_CTX");
32 return "";
33 }
34
35 if (EVP_DigestInit_ex(mdctx.get(), EVP_sha256(), nullptr) != 1) {
36 Log::GetLog()->error("Error initializing SHA-256 context");
37 return "";
38 }
39
40 if (EVP_DigestUpdate(mdctx.get(), buffer.data(), fileSize) != 1) {
41 Log::GetLog()->error("Error updating SHA-256 context");
42 return "";
43 }
44
45 unsigned char digest[EVP_MAX_MD_SIZE];
46 unsigned int digestLen;
47 if (EVP_DigestFinal_ex(mdctx.get(), digest, &digestLen) != 1) {
48 Log::GetLog()->error("Error finalizing SHA-256 context");
49 return "";
50 }
51
52 std::string result;
53 result.reserve(digestLen * 2);
54 for (unsigned int i = 0; i < digestLen; i++) {
55 char hex[3];
56 snprintf(hex, sizeof(hex), "%02x", digest[i]);
57 result += hex;
58 }
59
60 return result;
61 }
62
63 void saveToFile(const std::filesystem::path& filename, const std::string& content)
64 {
65 std::ofstream file(filename, std::ios::binary | std::ios::trunc);
66 if (file.is_open()) {
67 file.write(content.data(), content.size());
68 file.close();
69 return;
70 }
71
72 Log::GetLog()->error("Error opening file for writing: " + filename.string());
73 }
74
76 {
77 std::ifstream file(filename, std::ios::binary);
78 if (file.is_open()) {
79 std::string content;
80 file.seekg(0, std::ios::end);
81 content.resize(file.tellg());
82 file.seekg(0, std::ios::beg);
83 file.read(&content[0], content.size());
84 return content;
85 }
86
87 if (std::filesystem::exists(filename))
88 Log::GetLog()->error("Error file exists but is not readable: " + filename.string());
89
90 return "";
91 }
92
93 void saveToFilePlain(const std::filesystem::path& filename, const std::unordered_map<std::string, intptr_t>& map)
94 {
95 std::ofstream file(filename, std::ios::trunc);
96 if (file.is_open())
97 {
98 std::vector<std::pair<std::string, intptr_t>> sortedVec(map.begin(), map.end());
99 std::sort(sortedVec.begin(), sortedVec.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
100
101 for (const auto& pair : sortedVec)
102 {
103 std::string data(fmt::format("{}\n", pair.first));
104 file.write(data.data(), data.size());
105 }
106 file.close();
107 return;
108 }
109
110 Log::GetLog()->error("Error opening file for writing: " + filename.string());
111 }
112
114 {
115 std::unordered_set<std::string> set;
116
117 std::ifstream file(filename);
118 if (!file.is_open())
119 return default_filters;
120
121 std::string line;
122 while (std::getline(file, line))
123 {
124 set.insert(line);
125 }
126
127 file.close();
128 return set;
129 }
130}
std::string calculateSHA256(const std::filesystem::path &filename)
Definition Cache.cpp:13
void saveToFile(const std::filesystem::path &filename, const std::string &content)
Definition Cache.cpp:63
std::string readFromFile(const std::filesystem::path &filename)
Definition Cache.cpp:75
void saveToFilePlain(const std::filesystem::path &filename, const std::unordered_map< std::string, intptr_t > &map)
Definition Cache.cpp:93
std::unordered_set< std::string > readFileIntoSet(const std::filesystem::path &filename)
Definition Cache.cpp:113