source: trunk/python2/regfi/regfi.c @ 199

Last change on this file since 199 was 199, checked in by tim, 14 years ago

reworked part of regfi C API to make python wrappers simpler
continued work on python wrappers
fixed some issues in pyregfi-smoketest. WIP

File size: 6.1 KB
Line 
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
13static 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
22static 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    /*char* e = regfi_log_get_str();*/
34    /*fprintf(stderr, "%p\n", e);*/
35    goto error;
36  }
37
38  talloc_set_destructor((void *)self, RegistryFile_dest);
39  return self;
40
41 error:
42  talloc_free(self);
43  return NULL;
44}
45
46static KeyIterator RegistryFile_get_key(RegistryFile self, char **path, REGFI_ENCODING encoding) {
47  return CONSTRUCT(KeyIterator, KeyIterator, Con, NULL, self, path, encoding);
48}
49
50
51VIRTUAL(RegistryFile, Object) {
52  VMETHOD(Con) = RegistryFile_Con;
53  VMETHOD(get_key) = RegistryFile_get_key;
54} END_VIRTUAL
55
56static int KeyIterator_dest(void *self) {
57  KeyIterator this = (KeyIterator)self;
58
59  regfi_iterator_free(this->iter);
60  return 0;
61}
62
63static KeyIterator KeyIterator_Con(KeyIterator self, RegistryFile file, char **path,
64                             REGFI_ENCODING encoding) {
65  self->iter = regfi_iterator_new(file->reg, encoding);
66
67  if(!self->iter) {
68    RaiseError(ERuntimeError, "Error: %s", regfi_log_get_str());
69    goto error;
70  }
71
72  talloc_set_destructor((void*)self, KeyIterator_dest);
73
74  /* Traverse to the path */
75  if(path[0]) {
76    if(!regfi_iterator_walk_path(self->iter, (const char **)path)) {
77      RaiseError(ERuntimeError, "Unable to walk down key path");
78      goto error;
79    }
80  }
81
82  fprintf(stderr, "Con called\n");
83  self->first_called = false;
84
85  return self;
86 error:
87  return NULL;
88}
89
90static KeyIterator KeyIterator__iter__(KeyIterator self) {
91  return self;
92}
93
94
95static const REGFI_NK_REC* KeyIterator_next(KeyIterator self) {
96
97  fprintf(stderr, "next called\n");
98
99  if(!self->first_called)
100  {
101    regfi_iterator_first_subkey(self->iter);
102    self->first_called = true;
103  }
104  else
105    regfi_iterator_next_subkey(self->iter);
106   
107  return regfi_iterator_cur_subkey(self->iter);
108}
109
110
111static int KeyIterator_down(KeyIterator self) {
112  fprintf(stderr, "down cur_subkey: %d\n", self->iter->cur_subkey);
113  int result = regfi_iterator_down(self->iter);
114  fprintf(stderr, "down result: %d\n", result);
115  regfi_iterator_first_subkey(self->iter);
116  regfi_iterator_first_value(self->iter);
117  return result;
118}
119
120static int KeyIterator_up(KeyIterator self) {
121  return regfi_iterator_up(self->iter);
122}
123
124static ValueIterator KeyIterator_list_values(KeyIterator self) {
125  return CONSTRUCT(ValueIterator, ValueIterator, Con, NULL, self);
126}
127
128VIRTUAL(KeyIterator, Object) {
129  VMETHOD(Con) = KeyIterator_Con;
130  VMETHOD(iternext) = KeyIterator_next;
131  VMETHOD(down) = KeyIterator_down;
132  VMETHOD(up) = KeyIterator_up;
133  VMETHOD(__iter__) = KeyIterator__iter__;
134  VMETHOD(list_values) = KeyIterator_list_values;
135} END_VIRTUAL
136
137static int ValueIterator_dest(void *self) {
138  ValueIterator this = (ValueIterator)self;
139
140  talloc_unlink(this, this->iter);
141
142  return 0;
143}
144
145static ValueIterator ValueIterator_Con(ValueIterator self, KeyIterator key) {
146  // Take a copy of the iterator
147  self->iter = key->iter;
148  talloc_reference(self, self->iter);
149
150  talloc_set_destructor((void *)self, ValueIterator_dest);
151
152  return self;
153}
154
155static ValueIterator ValueIterator__iter__(ValueIterator self) {
156  return self;
157}
158
159static RawData ValueIterator_iternext(ValueIterator self) {
160  RawData result;
161  const REGFI_DATA* data;
162  const REGFI_VK_REC* rec;
163
164  if(!self->first_called)
165  {
166    regfi_iterator_first_value(self->iter);
167    self->first_called = true;
168  }
169  else
170    regfi_iterator_next_value(self->iter);
171
172  rec = regfi_iterator_cur_value(self->iter);
173
174  if(!rec) return NULL;
175
176  /* XXX: shouldn't parse data here every time we walk over a value. 
177   *      Instead, make data fetching a method and parse it then.
178   */
179  data = (REGFI_DATA *)regfi_iterator_fetch_data(self->iter, rec);
180  if(!data) {
181    RaiseError(ERuntimeError, "Unable to fetch data: %s", regfi_log_get_str());
182    goto error;
183  }
184
185  switch(rec->type) {
186  case REG_EXPAND_SZ:
187  case REG_SZ:
188    result = (RawData)CONSTRUCT(DataString, RawData, Con, NULL, data, rec);
189    break;
190
191  case REG_DWORD:
192    result = (RawData)CONSTRUCT(DWORDData, RawData, Con, NULL, data, rec);
193    break;
194
195  case REG_BINARY:
196  default:
197    result = (RawData)CONSTRUCT(RawData, RawData, Con, NULL, data, rec);
198    break;
199  }
200
201  return result;
202 error:
203  talloc_free(self);
204  return NULL;
205}
206
207VIRTUAL(ValueIterator, Object) {
208  VMETHOD(Con) = ValueIterator_Con;
209  VMETHOD(__iter__) = ValueIterator__iter__;
210  VMETHOD(iternext) = ValueIterator_iternext;
211} END_VIRTUAL
212
213static int RawData_dest(void *self) {
214  RawData this = (RawData)self;
215
216  if(this->data) {
217    regfi_free_record(this->data);
218  };
219
220  if(this->rec) {
221    regfi_free_record(this->rec);
222  };
223
224  return 0;
225}
226
227static RawData RawData_Con(RawData self, REGFI_DATA *data, REGFI_VK_REC *record) {
228  self->rec = record;
229  self->data = data;
230
231  talloc_set_destructor((void *)self, RawData_dest);
232
233  return self;
234}
235
236static int RawData_get_value(RawData self, char *buff, int len) {
237  int available_to_read = min(len, self->data->interpreted_size);
238
239  memcpy(buff, self->data->raw, available_to_read);
240
241  return available_to_read;
242}
243
244VIRTUAL(RawData, Object) {
245  VMETHOD(Con) = RawData_Con;
246  VMETHOD(get_value) = RawData_get_value;
247} END_VIRTUAL
248
249static char* DataString_get_value(DataString self) {
250  RawData this = (RawData)self;
251
252  return (char*)this->data->interpreted.string;
253}
254
255VIRTUAL(DataString, RawData) {
256  VMETHOD(get_value) = DataString_get_value;
257} END_VIRTUAL
258
259static uint64_t DWORDData_get_value(DWORDData self) {
260  RawData this = (RawData)self;
261
262  return this->data->interpreted.dword;
263}
264
265VIRTUAL(DWORDData, RawData) {
266  VMETHOD(get_value) = DWORDData_get_value;
267} END_VIRTUAL
268
269void pyregfi_init() {
270  regfi_init();
271  INIT_CLASS(RegistryFile);
272}
Note: See TracBrowser for help on using the repository browser.