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