]> git.cameronkatri.com Git - ldid.git/blob - ldid.hpp
Fix Authority when using -H
[ldid.git] / ldid.hpp
1 #ifndef LDID_HPP
2 #define LDID_HPP
3
4 /* SPDX-License-Identifier: AGPL-3.0-only */
5
6 #include <cstdlib>
7 #include <map>
8 #include <set>
9 #include <sstream>
10 #include <streambuf>
11 #include <string>
12 #include <vector>
13
14 namespace ldid {
15
16 // I wish Apple cared about providing quality toolchains :/
17
18 template <typename Function_>
19 class Functor;
20
21 template <typename Type_, typename... Args_>
22 class Functor<Type_ (Args_...)> {
23 public:
24 virtual Type_ operator ()(Args_... args) const = 0;
25 };
26
27 template <typename Function_>
28 class FunctorImpl;
29
30 template <typename Value_, typename Type_, typename... Args_>
31 class FunctorImpl<Type_ (Value_::*)(Args_...) const> :
32 public Functor<Type_ (Args_...)>
33 {
34 private:
35 const Value_ *value_;
36
37 public:
38 FunctorImpl(const Value_ &value) :
39 value_(&value)
40 {
41 }
42
43 virtual Type_ operator ()(Args_... args) const {
44 return (*value_)(args...);
45 }
46 };
47
48 template <typename Function_>
49 FunctorImpl<decltype(&Function_::operator())> fun(const Function_ &value) {
50 return value;
51 }
52
53 struct Progress {
54 virtual void operator()(const std::string &value) const = 0;
55 virtual void operator()(double value) const = 0;
56 };
57
58 class Folder {
59 public:
60 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) = 0;
61 virtual bool Look(const std::string &path) const = 0;
62 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const = 0;
63 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;
64 };
65
66 class DiskFolder :
67 public Folder
68 {
69 private:
70 const std::string path_;
71 std::map<std::string, std::string> commit_;
72
73 protected:
74 std::string Path(const std::string &path) const;
75
76 private:
77 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;
78
79 public:
80 DiskFolder(const std::string &path);
81 ~DiskFolder();
82
83 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
84 virtual bool Look(const std::string &path) const;
85 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
86 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;
87 };
88
89 class SubFolder :
90 public Folder
91 {
92 private:
93 Folder &parent_;
94 std::string path_;
95
96 public:
97 SubFolder(Folder &parent, const std::string &path);
98
99 std::string Path(const std::string &path) const;
100
101 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
102 virtual bool Look(const std::string &path) const;
103 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
104 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;
105 };
106
107 class UnionFolder :
108 public Folder
109 {
110 private:
111 struct Reset {
112 const void *flag_;
113 std::streambuf *data_;
114 };
115
116 Folder &parent_;
117 std::set<std::string> deletes_;
118
119 std::map<std::string, std::string> remaps_;
120 mutable std::map<std::string, Reset> resets_;
121
122 std::string Map(const std::string &path) const;
123 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;
124
125 public:
126 UnionFolder(Folder &parent);
127
128 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
129 virtual bool Look(const std::string &path) const;
130 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
131 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;
132
133 void operator ()(const std::string &from) {
134 deletes_.insert(from);
135 }
136
137 void operator ()(const std::string &from, const std::string &to) {
138 operator ()(from);
139 remaps_[to] = from;
140 }
141
142 void operator ()(const std::string &from, const void *flag, std::streambuf &data) {
143 operator ()(from);
144 auto &reset(resets_[from]);
145 reset.flag_ = flag;
146 reset.data_ = &data;
147 }
148 };
149
150 struct Hash {
151 uint8_t sha1_[0x14];
152 uint8_t sha256_[0x20];
153 };
154
155 struct Bundle {
156 std::string path;
157 Hash hash;
158 };
159
160 Bundle Sign(const std::string &root, Folder &folder, const std::string &key, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress);
161
162 typedef std::map<uint32_t, Hash> Slots;
163
164 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 std::string &key, const Slots &slots, uint32_t flags, bool platform, const Progress &progress);
165
166 }
167
168 #endif//LDID_HPP