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