1 | /* |
---|
2 | ** regfi.c |
---|
3 | ** |
---|
4 | ** Made by (mic) |
---|
5 | ** Login <mic@laptop> |
---|
6 | ** |
---|
7 | ** Started on Fri Apr 30 02:06:28 2010 mic |
---|
8 | ** Last update Sun May 12 01:17:25 2002 Speed Blue |
---|
9 | */ |
---|
10 | |
---|
11 | #include "pyregfi.h" |
---|
12 | |
---|
13 | static int RegistryFile_dest(void *self) { |
---|
14 | RegistryFile this = (RegistryFile)self; |
---|
15 | |
---|
16 | regfi_free(this->reg); |
---|
17 | close(this->fd); |
---|
18 | |
---|
19 | return 0; |
---|
20 | }; |
---|
21 | |
---|
22 | static RegistryFile RegistryFile_Con(RegistryFile self, char *filename) { |
---|
23 | self->fd = open(filename, O_RDONLY); |
---|
24 | if(self->fd < 0) { |
---|
25 | RaiseError(EIOError, "Unable to open %s", filename); |
---|
26 | goto error; |
---|
27 | }; |
---|
28 | |
---|
29 | self->reg = regfi_alloc(self->fd); |
---|
30 | |
---|
31 | if(!self->reg) { |
---|
32 | RaiseError(ERuntimeError, "REGFI Error: %s", regfi_log_get_str()); |
---|
33 | goto error; |
---|
34 | }; |
---|
35 | |
---|
36 | talloc_set_destructor((void *)self, RegistryFile_dest); |
---|
37 | return self; |
---|
38 | |
---|
39 | error: |
---|
40 | talloc_free(self); |
---|
41 | return NULL; |
---|
42 | }; |
---|
43 | |
---|
44 | static KeyIterator RegistryFile_get_key(RegistryFile self, char **path, REGFI_ENCODING encoding) { |
---|
45 | return CONSTRUCT(KeyIterator, KeyIterator, Con, NULL, self, path, encoding); |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | VIRTUAL(RegistryFile, Object) { |
---|
50 | VMETHOD(Con) = RegistryFile_Con; |
---|
51 | VMETHOD(get_key) = RegistryFile_get_key; |
---|
52 | } END_VIRTUAL |
---|
53 | |
---|
54 | static int KeyIterator_dest(void *self) { |
---|
55 | KeyIterator this = (KeyIterator)self; |
---|
56 | |
---|
57 | regfi_iterator_free(this->iter); |
---|
58 | if(this->next_item) { |
---|
59 | regfi_free_record(this->next_item); |
---|
60 | }; |
---|
61 | |
---|
62 | return 0; |
---|
63 | }; |
---|
64 | |
---|
65 | static KeyIterator KeyIterator_Con(KeyIterator self, RegistryFile file, char **path, |
---|
66 | REGFI_ENCODING encoding) { |
---|
67 | self->iter = regfi_iterator_new(file->reg, encoding); |
---|
68 | |
---|
69 | if(!self->iter) { |
---|
70 | RaiseError(ERuntimeError, "Error: %s", regfi_log_get_str()); |
---|
71 | goto error; |
---|
72 | }; |
---|
73 | |
---|
74 | talloc_set_destructor((void*)self, KeyIterator_dest); |
---|
75 | |
---|
76 | // Traverse to the path |
---|
77 | if(path[0]) { |
---|
78 | if(!regfi_iterator_walk_path(self->iter, (const char **)path)) { |
---|
79 | RaiseError(ERuntimeError, "Unable to walk down key path"); |
---|
80 | goto error; |
---|
81 | }; |
---|
82 | }; |
---|
83 | |
---|
84 | // Get the first key in the list |
---|
85 | self->next_item = regfi_iterator_first_subkey(self->iter); |
---|
86 | |
---|
87 | return self; |
---|
88 | error: |
---|
89 | return NULL; |
---|
90 | }; |
---|
91 | |
---|
92 | static void KeyIterator__iter__(KeyIterator self) { |
---|
93 | if(self->next_item) { |
---|
94 | regfi_free_record(self->next_item); |
---|
95 | }; |
---|
96 | |
---|
97 | self->next_item = regfi_iterator_first_subkey(self->iter); |
---|
98 | }; |
---|
99 | |
---|
100 | static REGFI_NK_REC *KeyIterator_next(KeyIterator self) { |
---|
101 | const REGFI_NK_REC * result; |
---|
102 | |
---|
103 | if(!self->next_item) return NULL; |
---|
104 | |
---|
105 | result = self->next_item; |
---|
106 | |
---|
107 | self->next_item = regfi_iterator_next_subkey(self->iter); |
---|
108 | |
---|
109 | return result; |
---|
110 | }; |
---|
111 | |
---|
112 | static ValueIterator KeyIterator_list_values(KeyIterator self) { |
---|
113 | return CONSTRUCT(ValueIterator, ValueIterator, Con, NULL, self); |
---|
114 | }; |
---|
115 | |
---|
116 | VIRTUAL(KeyIterator, Object) { |
---|
117 | VMETHOD(Con) = KeyIterator_Con; |
---|
118 | VMETHOD(iternext) = KeyIterator_next; |
---|
119 | VMETHOD(__iter__) = KeyIterator__iter__; |
---|
120 | VMETHOD(list_values) = KeyIterator_list_values; |
---|
121 | } END_VIRTUAL |
---|
122 | |
---|
123 | static int ValueIterator_dest(void *self) { |
---|
124 | ValueIterator this = (ValueIterator)self; |
---|
125 | |
---|
126 | if(this->next_item) regfi_free_record(this->next_item); |
---|
127 | |
---|
128 | return 0; |
---|
129 | }; |
---|
130 | |
---|
131 | static ValueIterator ValueIterator_Con(ValueIterator self, KeyIterator key) { |
---|
132 | // Take a copy of the iterator |
---|
133 | self->iter = key->iter; |
---|
134 | talloc_reference(self, self->iter); |
---|
135 | |
---|
136 | self->next_item = regfi_iterator_first_value(self->iter); |
---|
137 | |
---|
138 | talloc_set_destructor((void *)self, ValueIterator_dest); |
---|
139 | |
---|
140 | return self; |
---|
141 | }; |
---|
142 | |
---|
143 | static void ValueIterator__iter__(ValueIterator self) { |
---|
144 | if(self->next_item) regfi_free_record(self->next_item); |
---|
145 | |
---|
146 | self->next_item = regfi_iterator_first_value(self->iter); |
---|
147 | }; |
---|
148 | |
---|
149 | static RawData ValueIterator_iternext(ValueIterator self) { |
---|
150 | RawData result; |
---|
151 | REGFI_DATA *data; |
---|
152 | REGFI_VK_REC *rec = (REGFI_VK_REC *)self->next_item; |
---|
153 | |
---|
154 | if(!rec) return NULL; |
---|
155 | |
---|
156 | data = (REGFI_DATA *)regfi_iterator_fetch_data(self->iter, rec); |
---|
157 | if(!data) { |
---|
158 | RaiseError(ERuntimeError, "Unable to fetch data: %s", regfi_log_get_str()); |
---|
159 | goto error; |
---|
160 | }; |
---|
161 | |
---|
162 | switch(self->next_item->type) { |
---|
163 | case REG_EXPAND_SZ: |
---|
164 | case REG_SZ: |
---|
165 | result = (RawData)CONSTRUCT(DataString, RawData, Con, NULL, data, rec); |
---|
166 | break; |
---|
167 | |
---|
168 | case REG_DWORD: |
---|
169 | result = (RawData)CONSTRUCT(DWORDData, RawData, Con, NULL, data, rec); |
---|
170 | break; |
---|
171 | |
---|
172 | case REG_BINARY: |
---|
173 | default: |
---|
174 | result = (RawData)CONSTRUCT(RawData, RawData, Con, NULL, data, rec); |
---|
175 | break; |
---|
176 | }; |
---|
177 | |
---|
178 | |
---|
179 | /* if(self->next_item) { |
---|
180 | regfi_free_record(self->next_item); |
---|
181 | }; |
---|
182 | */ |
---|
183 | |
---|
184 | self->next_item = regfi_iterator_next_value(self->iter); |
---|
185 | |
---|
186 | return result; |
---|
187 | error: |
---|
188 | talloc_free(self); |
---|
189 | return NULL; |
---|
190 | }; |
---|
191 | |
---|
192 | VIRTUAL(ValueIterator, Object) { |
---|
193 | VMETHOD(Con) = ValueIterator_Con; |
---|
194 | VMETHOD(__iter__) = ValueIterator__iter__; |
---|
195 | VMETHOD(iternext) = ValueIterator_iternext; |
---|
196 | } END_VIRTUAL |
---|
197 | |
---|
198 | static int RawData_dest(void *self) { |
---|
199 | RawData this = (RawData)self; |
---|
200 | |
---|
201 | if(this->data) { |
---|
202 | regfi_free_record(this->data); |
---|
203 | }; |
---|
204 | |
---|
205 | if(this->rec) { |
---|
206 | regfi_free_record(this->rec); |
---|
207 | }; |
---|
208 | |
---|
209 | return 0; |
---|
210 | }; |
---|
211 | |
---|
212 | static RawData RawData_Con(RawData self, REGFI_DATA *data, REGFI_VK_REC *record) { |
---|
213 | self->rec = record; |
---|
214 | self->data = data; |
---|
215 | |
---|
216 | talloc_set_destructor((void *)self, RawData_dest); |
---|
217 | |
---|
218 | return self; |
---|
219 | }; |
---|
220 | |
---|
221 | static int RawData_get_value(RawData self, char *buff, int len) { |
---|
222 | int available_to_read = min(len, self->data->interpreted_size); |
---|
223 | |
---|
224 | memcpy(buff, self->data->raw, available_to_read); |
---|
225 | |
---|
226 | return available_to_read; |
---|
227 | }; |
---|
228 | |
---|
229 | VIRTUAL(RawData, Object) { |
---|
230 | VMETHOD(Con) = RawData_Con; |
---|
231 | VMETHOD(get_value) = RawData_get_value; |
---|
232 | } END_VIRTUAL |
---|
233 | |
---|
234 | static char *DataString_get_value(DataString self) { |
---|
235 | RawData this = (RawData)self; |
---|
236 | |
---|
237 | return this->data->interpreted.string; |
---|
238 | }; |
---|
239 | |
---|
240 | VIRTUAL(DataString, RawData) { |
---|
241 | VMETHOD(get_value) = DataString_get_value; |
---|
242 | } END_VIRTUAL |
---|
243 | |
---|
244 | static uint64_t DWORDData_get_value(DWORDData self) { |
---|
245 | RawData this = (RawData)self; |
---|
246 | |
---|
247 | return this->data->interpreted.dword; |
---|
248 | }; |
---|
249 | |
---|
250 | VIRTUAL(DWORDData, RawData) { |
---|
251 | VMETHOD(get_value) = DWORDData_get_value; |
---|
252 | } END_VIRTUAL |
---|
253 | |
---|
254 | void pyregfi_init() { |
---|
255 | INIT_CLASS(RegistryFile); |
---|
256 | |
---|
257 | }; |
---|