This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.cpp
147 lines (123 loc) · 3.29 KB
/
helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "helpers.h"
std::string uint16ToHexStr(uint16_t num)
{
std::stringstream stream;
stream << std::hex << num;
std::string hexStr = stream.str();
if (hexStr.size() % 4 != 0)
hexStr = std::string(4 - (hexStr.size() % 4), '0').append(hexStr);
return hexStr;
}
std::string uint32ToHexStr(uint32_t num)
{
std::stringstream stream;
stream << std::hex << swapUInt32Endianness(num);
std::string hexStr = stream.str();
if (hexStr.size() % 8 != 0)
hexStr = std::string(8 - (hexStr.size() % 8), '0').append(hexStr);
return hexStr;
}
std::string getReferenceFromHash(std::string hash, std::string pkgsPath)
{
Package pkg(getPkgID(hash), pkgsPath);
std::string reference = pkg.getEntryReference(hash);
return reference;
}
std::string getPkgID(std::string hash)
{
std::string pkgID = uint16ToHexStr(floor((hexStrToUint32(hash) - 0x80800000) / 8192));
return pkgID;
}
uint16_t getPkgID(uint32_t hash)
{
uint16_t pkgID = floor((hash - 0x80800000) / 8192);
return pkgID;
}
uint32_t hexStrToUint16(std::string hash)
{
return swapUInt16Endianness(std::stoul(hash, nullptr, 16));
}
uint32_t hexStrToUint32(std::string hash)
{
return swapUInt32Endianness(std::stoul(hash, nullptr, 16));
}
uint64_t hexStrToUint64(std::string hash)
{
return swapUInt64Endianness(std::stoull(hash, nullptr, 16));
}
uint16_t swapUInt16Endianness(uint16_t x)
{
x = (x << 8) + (x >> 8);
return x;
}
uint32_t swapUInt32Endianness(uint32_t x)
{
x = (x >> 24) |
((x << 8) & 0x00FF0000) |
((x >> 8) & 0x0000FF00) |
(x << 24);
return x;
}
uint64_t swapUInt64Endianness(uint64_t k)
{
return ((k << 56) |
((k & 0x000000000000FF00) << 40) |
((k & 0x0000000000FF0000) << 24) |
((k & 0x00000000FF000000) << 8) |
((k & 0x000000FF00000000) >> 8) |
((k & 0x0000FF0000000000) >> 24) |
((k & 0x00FF000000000000) >> 40) |
(k >> 56)
);
}
std::string getFileFromHash(std::string hsh)
{
uint32_t first_int = hexStrToUint32(hsh);
uint32_t one = first_int - 2155872256;
std::string first_hex = uint16ToHexStr(floor(one / 8192));
std::string second_hex = uint16ToHexStr(first_int % 8192);
return(first_hex + "-" + second_hex);
}
std::string load3(const std::string& path) {
auto close_file = [](FILE* f) {fclose(f); };
#pragma warning(suppress : 4996)
auto holder = std::unique_ptr<FILE, decltype(close_file)>(fopen(path.c_str(), "rb"), close_file);
if (!holder)
return "";
FILE* f = holder.get();
// in C++17 following lines can be folded into std::filesystem::file_size invocation
int size = std::filesystem::file_size(path);
std::string res;
res.resize(size);
// C++17 defines .data() which returns a non-const pointer
fread(const_cast<char*>(res.data()), 1, size, f);
return res;
}
void filePutContents(const std::string& name, const std::string& content) {
std::ofstream outfile;
outfile.open(name, std::ios_base::app);
outfile << content;
}
std::string getHash64(uint64_t hash64, std::unordered_map<uint64_t, uint32_t> hash64Table)
{
std::string h64 = "";
try
{
h64 = uint32ToHexStr(hash64Table[hash64]);
if (h64 == "00000000")
throw h64;
}
catch (std::string err)
{
std::cerr << "H64 file is out-of-date. Please delete and retry.\n";
exit(1);
}
return h64;
}
std::string to_str(double a_value)
{
double a = a_value;
std::stringstream out;
out << std::fixed << std::setprecision(6) << a;
return out.str();
}