]> git.saurik.com Git - ldid.git/blob - ldid.hpp
Add #include <set> that was semi-missing from hpp.
[ldid.git] / ldid.hpp
1 #ifndef LDID_HPP
2 #define LDID_HPP
3
4 #include <cstdlib>
5 #include <map>
6 #include <set>
7 #include <sstream>
8 #include <streambuf>
9 #include <string>
10 #include <vector>
11
12 namespace ldid {
13
14 // I wish Apple cared about providing quality toolchains :/
15
16 template <typename Function_>
17 class Functor;
18
19 template <typename Type_, typename... Args_>
20 class Functor<Type_ (Args_...)> {
21 public:
22 virtual Type_ operator ()(Args_... args) const = 0;
23 };
24
25 template <typename Function_>
26 class FunctorImpl;
27
28 template <typename Value_, typename Type_, typename... Args_>
29 class FunctorImpl<Type_ (Value_::*)(Args_...) const> :
30 public Functor<Type_ (Args_...)>
31 {
32 private:
33 const Value_ *value_;
34
35 public:
36 FunctorImpl(const Value_ &value) :
37 value_(&value)
38 {
39 }
40
41 virtual Type_ operator ()(Args_... args) const {
42 return (*value_)(args...);
43 }
44 };
45
46 template <typename Function_>
47 FunctorImpl<decltype(&Function_::operator())> fun(const Function_ &value) {
48 return value;
49 }
50
51 class Folder {
52 public:
53 virtual void Save(const std::string &path, const void *flag, const Functor<void (std::streambuf &)> &code) = 0;
54 virtual bool Look(const std::string &path) = 0;
55 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, const void *)> &code) = 0;
56 virtual void Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) = 0;
57 };
58
59 class DiskFolder :
60 public Folder
61 {
62 private:
63 const std::string path_;
64 std::map<std::string, std::string> commit_;
65
66 std::string Path(const std::string &path);
67
68 void Find(const std::string &root, const std::string &base, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link);
69
70 public:
71 DiskFolder(const std::string &path);
72 ~DiskFolder();
73
74 virtual void Save(const std::string &path, const void *flag, const Functor<void (std::streambuf &)> &code);
75 virtual bool Look(const std::string &path);
76 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, const void *)> &code);
77 virtual void Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link);
78 };
79
80 class SubFolder :
81 public Folder
82 {
83 private:
84 Folder &parent_;
85 std::string path_;
86
87 public:
88 SubFolder(Folder &parent, const std::string &path);
89
90 virtual void Save(const std::string &path, const void *flag, const Functor<void (std::streambuf &)> &code);
91 virtual bool Look(const std::string &path);
92 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, const void *)> &code);
93 virtual void Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link);
94 };
95
96 class UnionFolder :
97 public Folder
98 {
99 private:
100 class StringBuffer :
101 public std::stringbuf
102 {
103 public:
104 StringBuffer() {
105 }
106
107 StringBuffer(const StringBuffer &rhs) :
108 std::stringbuf(rhs.str())
109 {
110 }
111 };
112
113 Folder &parent_;
114 std::set<std::string> deletes_;
115
116 std::map<std::string, std::string> remaps_;
117 std::map<std::string, std::pair<StringBuffer, const void *>> resets_;
118
119 std::string Map(const std::string &path);
120 void Map(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code, const std::string &file, const Functor<void (const Functor<void (std::streambuf &, const void *)> &)> &save);
121
122 public:
123 UnionFolder(Folder &parent);
124
125 virtual void Save(const std::string &path, const void *flag, const Functor<void (std::streambuf &)> &code);
126 virtual bool Look(const std::string &path);
127 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, const void *)> &code);
128 virtual void Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link);
129
130 void operator ()(const std::string &from) {
131 deletes_.insert(from);
132 }
133
134 void operator ()(const std::string &from, const std::string &to) {
135 operator ()(from);
136 remaps_[to] = from;
137 }
138
139 std::stringbuf &operator ()(const std::string &from, const void *flag) {
140 operator ()(from);
141 auto &reset(resets_[from]);
142 reset.second = flag;
143 return reset.first;
144 }
145 };
146
147 struct Bundle {
148 std::string path;
149 std::vector<char> hash;
150 };
151
152 Bundle Sign(const std::string &root, Folder &folder, const std::string &key, const std::string &entitlements, const std::string &requirement);
153
154 typedef std::map<uint32_t, std::vector<char>> Slots;
155
156 std::string Analyze(const void *data, size_t size);
157 std::vector<char> Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, const std::string &requirement, const std::string &key, const Slots &slots);
158
159 }
160
161 #endif//LDID_HPP