summaryrefslogtreecommitdiff
path: root/other/Kermit/include/Patch.hpp
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/Kermit/include/Patch.hpp
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/Kermit/include/Patch.hpp')
-rw-r--r--other/Kermit/include/Patch.hpp204
1 files changed, 204 insertions, 0 deletions
diff --git a/other/Kermit/include/Patch.hpp b/other/Kermit/include/Patch.hpp
new file mode 100644
index 0000000..a784f9c
--- /dev/null
+++ b/other/Kermit/include/Patch.hpp
@@ -0,0 +1,204 @@
1/*
2 * Patch.hpp:
3 * representation of a kernel patch.
4 * written by palmers / teso
5 */
6#ifndef __PATCH_C
7#define __PATCH_C
8
9#include <rwKernel.hpp>
10#include <SymbolTable.hpp>
11#include <SystemMap.hpp>
12#include <stoi16.hpp>
13#include <itos16.hpp>
14#include <utility>
15#include <functional>
16#include <algorithm>
17#include <list>
18#include <fstream>
19#include <string>
20#include <name2add.h>
21
22
23typedef pair<unsigned int, unsigned int> Addr2Addr;
24typedef list<Addr2Addr *> Addr2AddrList;
25
26Addr2AddrList *genReplaceValMap (SymbolTable *st);
27void genDummyValMap ();
28extern SystemMap DummyValMap;
29
30
31#define CLEAN 1
32#define LINKED 2
33#define APPLIED 4
34#define LFAILED 8
35#define AFAILED 16
36
37
38/**
39 * Representation of a kernel patch. A Patch is a amount of data, which is to be written
40 * to a given address. Patching means modification of kernel memory. Therefore, the data,
41 * which will be overwritten, is saved (before writting).
42 * Additionally the status of the Patch is tracked. Thus, you are able to undo, reapply
43 * and debug patches. The states a Patch must be in are:
44 * CLEAN (the patch was never touched)
45 * LINKED (it was linked without an error)
46 * APPLIED (it was applied without an error)
47 * LFAILED (linking failed)
48 * AFAILED (applying failed)
49 */
50class Patch
51{
52private:
53 int state;
54 unsigned short len;
55 unsigned char *back_data, *data, *overwr;
56 unsigned int address;
57 rwKernel *local_rw;
58
59 bool initObjects (unsigned char *, unsigned short, unsigned int, rwKernel *);
60 string state2string ();
61 void string2state (string);
62 string data2string (unsigned char *);
63 void string2data (string, unsigned char *);
64 void parse (string);
65
66public:
67/**
68 * Create, but init nothing.
69 */
70 Patch ();
71
72/**
73 * Create a patch with supplied data.
74 * @param data patch data.
75 * @param len length of patch data.
76 * @param addr memory address to where the data shall be written.
77 */
78 Patch (unsigned char *data, unsigned short len, unsigned int addr);
79
80/**
81 * Create a patch with supplied data. This constructor, compared with the above,
82 * will set a local reference to a rwKernel object.
83 * @param x pointer to a rwKernel object.
84 */
85 Patch (unsigned char *data, unsigned short len, unsigned int addr, rwKernel *x);
86
87/**
88 * Initialize the object from a string as created by dump ().
89 * @see dump()
90 */
91 Patch (string);
92
93/**
94 * Initialize the object from a string as created by dump ().
95 * @see dump()
96 */
97 Patch (string, rwKernel *);
98
99/**
100 * Foo.
101 */
102 ~Patch ();
103
104
105/**
106 * init object from a string.
107 */
108 void initFromString (string);
109
110/**
111 * Foo.
112 */
113 string getPatchAsString ();
114
115/**
116 * tells you if the patch data was modified. (e.g. by linking).
117 * @return true if backup data and data differ.
118 */
119 bool wasChanged ();
120
121/**
122 * @return true if the linking returned no error messages.
123 */
124 bool isLinked ();
125
126/**
127 * @return true if the applying was successful.
128 */
129 bool isApplied ();
130
131/**
132 * @return true if linking or applying failed.
133 */
134 bool isFailed ();
135
136/**
137 * @return true if the patch was not touched.
138 */
139 bool isClean ();
140
141/**
142 * @return the status.
143 */
144 int getState ();
145
146/**
147 * Restore patch data. Might be helpful if linking failed.
148 */
149 void restore ();
150
151/**
152 * Remove applied Patch (Undo changes done to memory).
153 */
154 bool remove ();
155
156/**
157 * Remove applied Patch (Undo changes done to memory).
158 */
159 bool remove (rwKernel *);
160
161/**
162 * Get a pointer to patch data.
163 */
164 unsigned char *getData ();
165
166/**
167 * Apply the patch to the kernel. Effectivly write the patch data to the supplied address.
168 * The method allows you to supply a a reference to a rwKernel object. you can supply on
169 * construction of the patch. However, there might be none at that time.
170 */
171 void apply (rwKernel *);
172
173/**
174 * Apply the patch to the kernel. Use this apply method if you supplied a reference to a
175 * rwKernel object at creation time.
176 */
177 void apply ();
178
179/**
180 * link the patch with the kernel. Replace all placeholders with real addresses.
181 */
182 void link (Addr2AddrList *);
183
184/**
185 * Dump patch information into a file. This will produce human readable output. It
186 * can be used e.g. for restoring and debugging. Because the output is line based
187 * and can be used to initialize a Patch object you are effecitvely able to reproduce
188 * patching sessions.
189 * @see Patch(string)
190 * @param file filename.
191 */
192 void dump (string file);
193
194/**
195 * Foo.
196 */
197 friend istream& operator>> (istream&, Patch&);
198
199/**
200 * Foo.
201 */
202 friend ostream& operator<< (ostream&, Patch&);
203};
204#endif /* __PATCH_C */