-
Notifications
You must be signed in to change notification settings - Fork 45
/
ldid.hpp
179 lines (139 loc) · 5.73 KB
/
ldid.hpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef LDID_HPP
#define LDID_HPP
/* SPDX-License-Identifier: AGPL-3.0-only */
#include <cstdlib>
#include <map>
#include <set>
#include <sstream>
#include <streambuf>
#include <string>
#include <vector>
#include <openssl/x509.h>
namespace ldid {
// I wish Apple cared about providing quality toolchains :/
template <typename Function_>
class Functor;
template <typename Type_, typename... Args_>
class Functor<Type_ (Args_...)> {
public:
virtual Type_ operator ()(Args_... args) const = 0;
};
template <typename Function_>
class FunctorImpl;
template <typename Value_, typename Type_, typename... Args_>
class FunctorImpl<Type_ (Value_::*)(Args_...) const> :
public Functor<Type_ (Args_...)>
{
private:
const Value_ *value_;
public:
FunctorImpl(const Value_ &value) :
value_(&value)
{
}
virtual Type_ operator ()(Args_... args) const {
return (*value_)(args...);
}
};
template <typename Function_>
FunctorImpl<decltype(&Function_::operator())> fun(const Function_ &value) {
return value;
}
struct Progress {
virtual void operator()(const std::string &value) const = 0;
virtual void operator()(double value) const = 0;
};
class Folder {
public:
virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) = 0;
virtual bool Look(const std::string &path) const = 0;
virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const = 0;
virtual void Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const = 0;
};
class DiskFolder :
public Folder
{
private:
const std::string path_;
std::map<std::string, std::string> commit_;
protected:
std::string Path(const std::string &path) const;
private:
void Find(const std::string &root, const std::string &base, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const;
public:
DiskFolder(const std::string &path);
~DiskFolder();
virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
virtual bool Look(const std::string &path) const;
virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
virtual void Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const;
};
class SubFolder :
public Folder
{
private:
Folder &parent_;
std::string path_;
public:
SubFolder(Folder &parent, const std::string &path);
std::string Path(const std::string &path) const;
virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
virtual bool Look(const std::string &path) const;
virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
virtual void Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const;
};
class UnionFolder :
public Folder
{
private:
struct Reset {
const void *flag_;
std::streambuf *data_;
};
Folder &parent_;
std::set<std::string> deletes_;
std::map<std::string, std::string> remaps_;
mutable std::map<std::string, Reset> resets_;
std::string Map(const std::string &path) const;
void Map(const std::string &path, const Functor<void (const std::string &)> &code, const std::string &file, const Functor<void (const Functor<void (std::streambuf &, size_t, const void *)> &)> &save) const;
public:
UnionFolder(Folder &parent);
virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
virtual bool Look(const std::string &path) const;
virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
virtual void Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const;
void operator ()(const std::string &from) {
deletes_.insert(from);
}
void operator ()(const std::string &from, const std::string &to) {
operator ()(from);
remaps_[to] = from;
}
void operator ()(const std::string &from, const void *flag, std::streambuf &data) {
operator ()(from);
auto &reset(resets_[from]);
reset.flag_ = flag;
reset.data_ = &data;
}
};
struct Hash {
uint8_t sha1_[0x14];
uint8_t sha256_[0x20];
};
struct Bundle {
std::string path;
Hash hash;
};
class Signer {
public:
virtual ~Signer() {};
virtual operator EVP_PKEY *() const = 0;
virtual operator X509 *() const = 0;
virtual operator STACK_OF(X509) *() const = 0;
virtual operator bool () const = 0;
};
Bundle Sign(const std::string &root, Folder &folder, const Signer &signer, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, bool merge, uint8_t platform, const Progress &progress);
typedef std::map<uint32_t, Hash> Slots;
Hash Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const Signer &signer, const Slots &slots, uint32_t flags, uint8_t platform, const Progress &progress);
}
#endif//LDID_HPP