]>
Commit | Line | Data |
---|---|---|
a0c715e9 JF |
1 | #ifndef LDID_HPP |
2 | #define LDID_HPP | |
3 | ||
4 | #include <cstdlib> | |
5 | #include <map> | |
fc01987c | 6 | #include <set> |
42d1472e | 7 | #include <sstream> |
a0c715e9 JF |
8 | #include <streambuf> |
9 | #include <string> | |
42d1472e | 10 | #include <vector> |
a0c715e9 JF |
11 | |
12 | namespace ldid { | |
13 | ||
e6a376fc JF |
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 | ||
b4b49374 JF |
51 | struct Progress { |
52 | virtual void operator()(const std::string &value) const = 0; | |
53 | virtual void operator()(double value) const = 0; | |
54 | }; | |
55 | ||
23c11ee8 JF |
56 | class Folder { |
57 | public: | |
addc31c5 JF |
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; | |
e1d26767 | 60 | virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const = 0; |
addc31c5 | 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; |
23c11ee8 JF |
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 | ||
d58cc49d | 71 | protected: |
addc31c5 | 72 | std::string Path(const std::string &path) const; |
23c11ee8 | 73 | |
d58cc49d | 74 | private: |
addc31c5 | 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; |
54a0f854 | 76 | |
23c11ee8 JF |
77 | public: |
78 | DiskFolder(const std::string &path); | |
79 | ~DiskFolder(); | |
80 | ||
addc31c5 JF |
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; | |
e1d26767 | 83 | virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const; |
addc31c5 | 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; |
23c11ee8 JF |
85 | }; |
86 | ||
87 | class SubFolder : | |
88 | public Folder | |
89 | { | |
90 | private: | |
2443500c | 91 | Folder &parent_; |
23c11ee8 JF |
92 | std::string path_; |
93 | ||
94 | public: | |
2443500c | 95 | SubFolder(Folder &parent, const std::string &path); |
23c11ee8 | 96 | |
90c0c6ef JF |
97 | std::string Path(const std::string &path) const; |
98 | ||
addc31c5 JF |
99 | virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code); |
100 | virtual bool Look(const std::string &path) const; | |
e1d26767 | 101 | virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const; |
addc31c5 | 102 | 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; |
23c11ee8 JF |
103 | }; |
104 | ||
886cb3f1 JF |
105 | class UnionFolder : |
106 | public Folder | |
107 | { | |
108 | private: | |
4e6d856a JF |
109 | struct Reset { |
110 | const void *flag_; | |
111 | std::streambuf *data_; | |
4d453c35 JF |
112 | }; |
113 | ||
886cb3f1 | 114 | Folder &parent_; |
7c49f771 JF |
115 | std::set<std::string> deletes_; |
116 | ||
117 | std::map<std::string, std::string> remaps_; | |
4e6d856a | 118 | mutable std::map<std::string, Reset> resets_; |
7c49f771 | 119 | |
addc31c5 | 120 | std::string Map(const std::string &path) const; |
e1d26767 | 121 | 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; |
886cb3f1 JF |
122 | |
123 | public: | |
124 | UnionFolder(Folder &parent); | |
125 | ||
addc31c5 JF |
126 | virtual void Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code); |
127 | virtual bool Look(const std::string &path) const; | |
e1d26767 | 128 | virtual void Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const; |
addc31c5 | 129 | 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; |
886cb3f1 | 130 | |
7c49f771 JF |
131 | void operator ()(const std::string &from) { |
132 | deletes_.insert(from); | |
133 | } | |
134 | ||
135 | void operator ()(const std::string &from, const std::string &to) { | |
136 | operator ()(from); | |
137 | remaps_[to] = from; | |
138 | } | |
139 | ||
4e6d856a | 140 | void operator ()(const std::string &from, const void *flag, std::streambuf &data) { |
fefcecb0 JF |
141 | operator ()(from); |
142 | auto &reset(resets_[from]); | |
4e6d856a JF |
143 | reset.flag_ = flag; |
144 | reset.data_ = &data; | |
886cb3f1 JF |
145 | } |
146 | }; | |
147 | ||
f74652ab JF |
148 | struct Hash { |
149 | uint8_t sha1_[0x14]; | |
150 | uint8_t sha256_[0x20]; | |
151 | }; | |
152 | ||
4ebf3494 JF |
153 | struct Bundle { |
154 | std::string path; | |
f74652ab | 155 | Hash hash; |
4ebf3494 JF |
156 | }; |
157 | ||
b4b49374 | 158 | 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); |
2dcf3faf | 159 | |
f74652ab | 160 | typedef std::map<uint32_t, Hash> Slots; |
a0c715e9 | 161 | |
ec3af45e | 162 | 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); |
a0c715e9 JF |
163 | |
164 | } | |
165 | ||
166 | #endif//LDID_HPP |