]> git.saurik.com Git - ldid.git/blob - ldid.hpp
a6a7b497b29e373c3535f6d2b3a11009f51574f5
[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 struct Progress {
52 virtual void operator()(const std::string &value) const = 0;
53 virtual void operator()(double value) const = 0;
54 };
55
56 class Folder {
57 public:
58 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) = 0;
59 virtual bool Look(const std::string &path) const = 0;
60 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const = 0;
61 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;
62 };
63
64 class DiskFolder :
65 public Folder
66 {
67 private:
68 const std::string path_;
69 std::map<std::string, std::string> commit_;
70
71 protected:
72 std::string Path(const std::string &path) const;
73
74 private:
75 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;
76
77 public:
78 DiskFolder(const std::string &path);
79 ~DiskFolder();
80
81 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
82 virtual bool Look(const std::string &path) const;
83 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
84 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;
85 };
86
87 class SubFolder :
88 public Folder
89 {
90 private:
91 Folder &parent_;
92 std::string path_;
93
94 public:
95 SubFolder(Folder &parent, const std::string &path);
96
97 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
98 virtual bool Look(const std::string &path) const;
99 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
100 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;
101 };
102
103 class UnionFolder :
104 public Folder
105 {
106 private:
107 struct Reset {
108 const void *flag_;
109 std::streambuf *data_;
110 };
111
112 Folder &parent_;
113 std::set<std::string> deletes_;
114
115 std::map<std::string, std::string> remaps_;
116 mutable std::map<std::string, Reset> resets_;
117
118 std::string Map(const std::string &path) const;
119 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;
120
121 public:
122 UnionFolder(Folder &parent);
123
124 virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code);
125 virtual bool Look(const std::string &path) const;
126 virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const;
127 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;
128
129 void operator ()(const std::string &from) {
130 deletes_.insert(from);
131 }
132
133 void operator ()(const std::string &from, const std::string &to) {
134 operator ()(from);
135 remaps_[to] = from;
136 }
137
138 void operator ()(const std::string &from, const void *flag, std::streambuf &data) {
139 operator ()(from);
140 auto &reset(resets_[from]);
141 reset.flag_ = flag;
142 reset.data_ = &data;
143 }
144 };
145
146 struct Hash {
147 uint8_t sha1_[0x14];
148 uint8_t sha256_[0x20];
149 };
150
151 struct Bundle {
152 std::string path;
153 Hash hash;
154 };
155
156 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);
157
158 typedef std::map<uint32_t, Hash> Slots;
159
160 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);
161
162 }
163
164 #endif//LDID_HPP