[204] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[210] | 3 | ## @package pyregfi |
---|
| 4 | # Python interface to the regfi library. |
---|
| 5 | # |
---|
| 6 | |
---|
[204] | 7 | import sys |
---|
[214] | 8 | import weakref |
---|
[204] | 9 | from pyregfi.structures import * |
---|
| 10 | |
---|
| 11 | import ctypes |
---|
| 12 | import ctypes.util |
---|
[207] | 13 | from ctypes import c_char,c_char_p,c_int,c_uint16,c_uint32,c_bool,POINTER |
---|
[204] | 14 | |
---|
| 15 | regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True) |
---|
| 16 | |
---|
| 17 | |
---|
[213] | 18 | regfi.regfi_alloc.argtypes = [c_int, REGFI_ENCODING] |
---|
[204] | 19 | regfi.regfi_alloc.restype = POINTER(REGFI_FILE) |
---|
| 20 | |
---|
[213] | 21 | regfi.regfi_alloc_cb.argtypes = [POINTER(REGFI_RAW_FILE), REGFI_ENCODING] |
---|
[204] | 22 | regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE) |
---|
| 23 | |
---|
[205] | 24 | regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)] |
---|
| 25 | regfi.regfi_free.restype = None |
---|
| 26 | |
---|
[204] | 27 | regfi.regfi_log_get_str.argtypes = [] |
---|
| 28 | regfi.regfi_log_get_str.restype = c_char_p |
---|
| 29 | |
---|
[205] | 30 | regfi.regfi_log_set_mask.argtypes = [c_uint16] |
---|
| 31 | regfi.regfi_log_set_mask.restype = c_bool |
---|
| 32 | |
---|
| 33 | regfi.regfi_free_record.argtypes = [c_void_p] |
---|
| 34 | regfi.regfi_free_record.restype = None |
---|
| 35 | |
---|
[207] | 36 | regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)] |
---|
| 37 | regfi.regfi_fetch_num_subkeys.restype = c_uint32 |
---|
| 38 | |
---|
| 39 | regfi.regfi_fetch_num_values.argtypes = [POINTER(REGFI_NK)] |
---|
| 40 | regfi.regfi_fetch_num_values.restype = c_uint32 |
---|
| 41 | |
---|
[206] | 42 | regfi.regfi_fetch_classname.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)] |
---|
| 43 | regfi.regfi_fetch_classname.restype = POINTER(REGFI_CLASSNAME) |
---|
| 44 | |
---|
| 45 | regfi.regfi_fetch_sk.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)] |
---|
| 46 | regfi.regfi_fetch_sk.restype = POINTER(REGFI_SK) |
---|
| 47 | |
---|
| 48 | regfi.regfi_fetch_data.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_VK)] |
---|
| 49 | regfi.regfi_fetch_data.restype = POINTER(REGFI_DATA) |
---|
| 50 | |
---|
[207] | 51 | regfi.regfi_find_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), |
---|
| 52 | c_char_p, POINTER(c_uint32)] |
---|
| 53 | regfi.regfi_find_subkey.restype = c_bool |
---|
| 54 | |
---|
| 55 | regfi.regfi_find_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), |
---|
| 56 | c_char_p, POINTER(c_uint32)] |
---|
| 57 | regfi.regfi_find_value.restype = c_bool |
---|
| 58 | |
---|
| 59 | regfi.regfi_get_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), |
---|
| 60 | c_uint32] |
---|
| 61 | regfi.regfi_get_subkey.restype = POINTER(REGFI_NK) |
---|
| 62 | |
---|
| 63 | regfi.regfi_get_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), |
---|
| 64 | c_uint32] |
---|
| 65 | regfi.regfi_get_value.restype = POINTER(REGFI_VK) |
---|
| 66 | |
---|
[205] | 67 | regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING] |
---|
| 68 | regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR) |
---|
| 69 | |
---|
| 70 | regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 71 | regfi.regfi_iterator_free.restype = None |
---|
| 72 | |
---|
| 73 | regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 74 | regfi.regfi_iterator_down.restype = c_bool |
---|
| 75 | |
---|
| 76 | regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 77 | regfi.regfi_iterator_up.restype = c_bool |
---|
| 78 | |
---|
| 79 | regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 80 | regfi.regfi_iterator_to_root.restype = c_bool |
---|
| 81 | |
---|
| 82 | regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 83 | regfi.regfi_iterator_walk_path.restype = c_bool |
---|
| 84 | |
---|
| 85 | regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 86 | regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK) |
---|
| 87 | |
---|
| 88 | regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 89 | regfi.regfi_iterator_first_subkey.restype = c_bool |
---|
| 90 | |
---|
| 91 | regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 92 | regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK) |
---|
| 93 | |
---|
| 94 | regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 95 | regfi.regfi_iterator_next_subkey.restype = c_bool |
---|
| 96 | |
---|
| 97 | regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] |
---|
| 98 | regfi.regfi_iterator_find_subkey.restype = c_bool |
---|
| 99 | |
---|
| 100 | regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 101 | regfi.regfi_iterator_first_value.restype = c_bool |
---|
| 102 | |
---|
| 103 | regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 104 | regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK) |
---|
| 105 | |
---|
| 106 | regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
| 107 | regfi.regfi_iterator_next_value.restype = c_bool |
---|
| 108 | |
---|
| 109 | regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] |
---|
| 110 | regfi.regfi_iterator_find_value.restype = c_bool |
---|
| 111 | |
---|
| 112 | |
---|
[204] | 113 | regfi.regfi_init.argtypes = [] |
---|
| 114 | regfi.regfi_init.restype = None |
---|
| 115 | regfi.regfi_init() |
---|
| 116 | |
---|
| 117 | |
---|
[210] | 118 | ## Retrieves messages produced by regfi during parsing and interpretation |
---|
| 119 | # |
---|
[205] | 120 | def GetLogMessages(): |
---|
[206] | 121 | msgs = regfi.regfi_log_get_str() |
---|
| 122 | if msgs == None: |
---|
| 123 | return '' |
---|
[207] | 124 | return msgs.decode('ascii') |
---|
[205] | 125 | |
---|
| 126 | |
---|
[208] | 127 | def _buffer2bytearray(char_pointer, length): |
---|
| 128 | if length == 0 or char_pointer == None: |
---|
| 129 | return None |
---|
| 130 | |
---|
| 131 | ret_val = bytearray(length) |
---|
| 132 | for i in range(0,length): |
---|
| 133 | ret_val[i] = char_pointer[i][0] |
---|
| 134 | |
---|
| 135 | return ret_val |
---|
| 136 | |
---|
| 137 | |
---|
[209] | 138 | def _charss2strlist(chars_pointer): |
---|
| 139 | ret_val = [] |
---|
| 140 | i = 0 |
---|
| 141 | s = chars_pointer[i] |
---|
| 142 | while s != None: |
---|
[213] | 143 | ret_val.append(s.decode('utf-8', 'replace')) |
---|
[209] | 144 | i += 1 |
---|
| 145 | s = chars_pointer[i] |
---|
[208] | 146 | |
---|
[209] | 147 | return ret_val |
---|
[208] | 148 | |
---|
[210] | 149 | |
---|
| 150 | ## Abstract class which Handles memory management and proxies attribute |
---|
| 151 | # access to base structures |
---|
[212] | 152 | class _StructureWrapper(object): |
---|
[214] | 153 | _hive = None |
---|
| 154 | _base = None |
---|
[206] | 155 | |
---|
[207] | 156 | def __init__(self, hive, base): |
---|
[214] | 157 | self._hive = hive |
---|
[206] | 158 | # XXX: check for NULL here, throw an exception if so. |
---|
[214] | 159 | self._base = base |
---|
[206] | 160 | |
---|
| 161 | def __del__(self): |
---|
[214] | 162 | regfi.regfi_free_record(self._base) |
---|
[213] | 163 | hive = None |
---|
[206] | 164 | |
---|
| 165 | def __getattr__(self, name): |
---|
[214] | 166 | return getattr(self._base.contents, name) |
---|
[206] | 167 | |
---|
| 168 | def __eq__(self, other): |
---|
| 169 | return (type(self) == type(other)) and (self.offset == other.offset) |
---|
| 170 | |
---|
| 171 | def __ne__(self, other): |
---|
| 172 | return (not self.__eq__(other)) |
---|
| 173 | |
---|
[210] | 174 | ## Registry key |
---|
[208] | 175 | class Key(_StructureWrapper): |
---|
| 176 | pass |
---|
| 177 | |
---|
[206] | 178 | class Value(_StructureWrapper): |
---|
| 179 | pass |
---|
| 180 | |
---|
[210] | 181 | ## Registry value data |
---|
[206] | 182 | class Data(_StructureWrapper): |
---|
| 183 | pass |
---|
| 184 | |
---|
[210] | 185 | ## Registry security record/permissions |
---|
[206] | 186 | class Security(_StructureWrapper): |
---|
| 187 | pass |
---|
| 188 | |
---|
[207] | 189 | |
---|
[212] | 190 | class _GenericList(object): |
---|
[214] | 191 | _hive = None |
---|
| 192 | _key = None |
---|
| 193 | _length = None |
---|
| 194 | _current = None |
---|
[207] | 195 | |
---|
[214] | 196 | # implementation-specific functions for _SubkeyList and _ValueList |
---|
| 197 | _fetch_num = None |
---|
| 198 | _find_element = None |
---|
| 199 | _get_element = None |
---|
| 200 | _constructor = None |
---|
[208] | 201 | |
---|
[207] | 202 | def __init__(self, key): |
---|
[214] | 203 | self._hive = key._hive |
---|
| 204 | |
---|
| 205 | # Normally it's good to avoid cyclic references like this |
---|
| 206 | # (key.list.key...) but in this case it makes ctypes memory |
---|
| 207 | # management easier to reference the Key instead of the base |
---|
| 208 | # structure. We use a weak reference in order to allow for garbage |
---|
| 209 | # collection, since value/subkey lists should not be usable if their |
---|
| 210 | # parent Key is freed anyway. |
---|
| 211 | |
---|
[207] | 212 | # XXX: check for NULL here, throw an exception if so. |
---|
[214] | 213 | self._key = weakref.proxy(key) |
---|
| 214 | self._length = self._fetch_num(key._base) |
---|
| 215 | |
---|
[207] | 216 | |
---|
| 217 | def __len__(self): |
---|
[214] | 218 | return self._length |
---|
[207] | 219 | |
---|
| 220 | def __getitem__(self, name): |
---|
| 221 | index = c_uint32() |
---|
[208] | 222 | if isinstance(name, str): |
---|
| 223 | name = name.encode('utf-8') |
---|
| 224 | |
---|
[209] | 225 | if name != None: |
---|
| 226 | name = create_string_buffer(bytes(name)) |
---|
| 227 | |
---|
[214] | 228 | if self._find_element(self._hive.file, self._key.base, name, byref(index)): |
---|
| 229 | return self._constructor(self._hive, |
---|
| 230 | self._get_element(self._hive.file, |
---|
| 231 | self._key.base, |
---|
| 232 | index)) |
---|
[207] | 233 | raise KeyError('') |
---|
| 234 | |
---|
[209] | 235 | def get(self, name, default): |
---|
| 236 | try: |
---|
| 237 | return self[name] |
---|
| 238 | except KeyError: |
---|
| 239 | return default |
---|
| 240 | |
---|
[207] | 241 | def __iter__(self): |
---|
[214] | 242 | self._current = 0 |
---|
[207] | 243 | return self |
---|
| 244 | |
---|
| 245 | def __next__(self): |
---|
[214] | 246 | if self._current >= self._length: |
---|
[207] | 247 | raise StopIteration('') |
---|
| 248 | |
---|
[214] | 249 | elem = self._get_element(self._hive.file, self._key._base, |
---|
| 250 | c_uint32(self._current)) |
---|
| 251 | self._current += 1 |
---|
| 252 | return self._constructor(self._hive, elem) |
---|
[207] | 253 | |
---|
[212] | 254 | # For Python 2.x |
---|
[214] | 255 | next = __next__ |
---|
[207] | 256 | |
---|
[212] | 257 | |
---|
[208] | 258 | class _SubkeyList(_GenericList): |
---|
[214] | 259 | _fetch_num = regfi.regfi_fetch_num_subkeys |
---|
| 260 | _find_element = regfi.regfi_find_subkey |
---|
| 261 | _get_element = regfi.regfi_get_subkey |
---|
[208] | 262 | |
---|
| 263 | |
---|
| 264 | class _ValueList(_GenericList): |
---|
[214] | 265 | _fetch_num = regfi.regfi_fetch_num_values |
---|
| 266 | _find_element = regfi.regfi_find_value |
---|
| 267 | _get_element = regfi.regfi_get_value |
---|
[208] | 268 | |
---|
| 269 | |
---|
[207] | 270 | class Key(_StructureWrapper): |
---|
| 271 | values = None |
---|
[208] | 272 | subkeys = None |
---|
[207] | 273 | |
---|
| 274 | def __init__(self, hive, base): |
---|
| 275 | super(Key, self).__init__(hive, base) |
---|
| 276 | self.values = _ValueList(self) |
---|
[208] | 277 | self.subkeys = _SubkeyList(self) |
---|
[207] | 278 | |
---|
[208] | 279 | def __getattr__(self, name): |
---|
| 280 | ret_val = super(Key, self).__getattr__(name) |
---|
| 281 | |
---|
| 282 | if name == "name": |
---|
[209] | 283 | if ret_val == None: |
---|
| 284 | ret_val = self.name_raw |
---|
| 285 | else: |
---|
[213] | 286 | ret_val = ret_val.decode('utf-8', 'replace') |
---|
[209] | 287 | |
---|
[208] | 288 | elif name == "name_raw": |
---|
| 289 | length = super(Key, self).__getattr__('name_length') |
---|
| 290 | ret_val = _buffer2bytearray(ret_val, length) |
---|
| 291 | |
---|
| 292 | return ret_val |
---|
| 293 | |
---|
| 294 | |
---|
[207] | 295 | def fetch_security(self): |
---|
[214] | 296 | return Security(self._hive, |
---|
| 297 | regfi.regfi_fetch_sk(self._hive.file, self.base)) |
---|
[207] | 298 | |
---|
| 299 | |
---|
[210] | 300 | ## Registry value (metadata) |
---|
| 301 | # |
---|
| 302 | # These represent registry values (@ref REGFI_VK records) and provide |
---|
| 303 | # access to their associated data. |
---|
| 304 | # |
---|
[208] | 305 | class Value(_StructureWrapper): |
---|
| 306 | def __getattr__(self, name): |
---|
[209] | 307 | ret_val = None |
---|
| 308 | if name == "data": |
---|
[214] | 309 | data_p = regfi.regfi_fetch_data(self._hive.file, self.base) |
---|
[209] | 310 | try: |
---|
| 311 | data_struct = data_p.contents |
---|
| 312 | except Exception: |
---|
| 313 | return None |
---|
[208] | 314 | |
---|
[209] | 315 | if data_struct.interpreted_size == 0: |
---|
| 316 | ret_val = None |
---|
| 317 | elif data_struct.type in (REG_SZ, REG_EXPAND_SZ, REG_LINK): |
---|
| 318 | # Unicode strings |
---|
[213] | 319 | ret_val = data_struct.interpreted.string.decode('utf-8', 'replace') |
---|
[209] | 320 | elif data_struct.type in (REG_DWORD, REG_DWORD_BE): |
---|
| 321 | # 32 bit integers |
---|
| 322 | ret_val = data_struct.interpreted.dword |
---|
| 323 | elif data_struct.type == REG_QWORD: |
---|
| 324 | # 64 bit integers |
---|
| 325 | ret_val = data_struct.interpreted.qword |
---|
| 326 | elif data_struct.type == REG_MULTI_SZ: |
---|
| 327 | ret_val = _charss2strlist(data_struct.interpreted.multiple_string) |
---|
| 328 | elif data_struct.type in (REG_NONE, REG_RESOURCE_LIST, |
---|
| 329 | REG_FULL_RESOURCE_DESCRIPTOR, |
---|
| 330 | REG_RESOURCE_REQUIREMENTS_LIST, |
---|
| 331 | REG_BINARY): |
---|
| 332 | ret_val = _buffer2bytearray(data_struct.interpreted.none, |
---|
| 333 | data_struct.interpreted_size) |
---|
| 334 | |
---|
| 335 | regfi.regfi_free_record(data_p) |
---|
| 336 | |
---|
| 337 | elif name == "data_raw": |
---|
| 338 | # XXX: should we load the data without interpretation instead? |
---|
[214] | 339 | data_p = regfi.regfi_fetch_data(self._hive.file, self.base) |
---|
[209] | 340 | try: |
---|
| 341 | data_struct = data_p.contents |
---|
| 342 | except Exception: |
---|
| 343 | return None |
---|
| 344 | |
---|
| 345 | ret_val = _buffer2bytearray(data_struct.raw, |
---|
| 346 | data_struct.size) |
---|
| 347 | regfi.regfi_free_record(data_p) |
---|
| 348 | |
---|
| 349 | else: |
---|
| 350 | ret_val = super(Value, self).__getattr__(name) |
---|
| 351 | if name == "name": |
---|
| 352 | if ret_val == None: |
---|
| 353 | ret_val = self.name_raw |
---|
| 354 | else: |
---|
[213] | 355 | ret_val = ret_val.decode('utf-8', 'replace') |
---|
[209] | 356 | |
---|
| 357 | elif name == "name_raw": |
---|
| 358 | length = super(Value, self).__getattr__('name_length') |
---|
| 359 | ret_val = _buffer2bytearray(ret_val, length) |
---|
| 360 | |
---|
[208] | 361 | return ret_val |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | # Avoids chicken/egg class definitions. |
---|
| 365 | # Also makes for convenient code reuse in these lists' parent classes. |
---|
[214] | 366 | _SubkeyList._constructor = Key |
---|
| 367 | _ValueList._constructor = Value |
---|
[208] | 368 | |
---|
| 369 | |
---|
| 370 | |
---|
[210] | 371 | ## Represents a single registry hive (file) |
---|
| 372 | # |
---|
| 373 | class Hive(): |
---|
[204] | 374 | file = None |
---|
| 375 | raw_file = None |
---|
| 376 | |
---|
| 377 | def __init__(self, fh): |
---|
[209] | 378 | # The fileno method may not exist, or it may throw an exception |
---|
[205] | 379 | # when called if the file isn't backed with a descriptor. |
---|
| 380 | try: |
---|
| 381 | if hasattr(fh, 'fileno'): |
---|
[213] | 382 | self.file = regfi.regfi_alloc(fh.fileno(), REGFI_ENCODING_UTF8) |
---|
[205] | 383 | return |
---|
| 384 | except: |
---|
| 385 | pass |
---|
[204] | 386 | |
---|
[205] | 387 | self.raw_file = structures.REGFI_RAW_FILE() |
---|
| 388 | self.raw_file.fh = fh |
---|
| 389 | self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek) |
---|
| 390 | self.raw_file.read = read_cb_type(self.raw_file.cb_read) |
---|
[213] | 391 | self.file = regfi.regfi_alloc_cb(self.raw_file, REGFI_ENCODING_UTF8) |
---|
[204] | 392 | |
---|
| 393 | def __getattr__(self, name): |
---|
| 394 | return getattr(self.file.contents, name) |
---|
[205] | 395 | |
---|
[210] | 396 | def __del__(self): |
---|
[205] | 397 | regfi.regfi_free(self.file) |
---|
| 398 | if self.raw_file != None: |
---|
[213] | 399 | self.raw_file = None |
---|
[204] | 400 | |
---|
[205] | 401 | def __iter__(self): |
---|
| 402 | return HiveIterator(self) |
---|
[204] | 403 | |
---|
[210] | 404 | ## Creates a @ref HiveIterator initialized at the specified path in |
---|
| 405 | # the hive. |
---|
| 406 | # |
---|
| 407 | # Raises an Exception if the path could not be found/traversed. |
---|
[206] | 408 | def subtree(self, path): |
---|
| 409 | hi = HiveIterator(self) |
---|
| 410 | hi.descend(path) |
---|
| 411 | return hi |
---|
[205] | 412 | |
---|
[206] | 413 | |
---|
[210] | 414 | ## A special purpose iterator for registry hives |
---|
| 415 | # |
---|
| 416 | # Iterating over an object of this type causes all keys in a specific |
---|
| 417 | # hive subtree to be returned in a depth-first manner. These iterators |
---|
| 418 | # are typically created using the @ref Hive.subtree() function on a @ref Hive |
---|
| 419 | # object. |
---|
| 420 | # |
---|
| 421 | # HiveIterators can also be used to manually traverse up and down a |
---|
| 422 | # registry hive as they retain information about the current position in |
---|
| 423 | # the hive, along with which iteration state for subkeys and values for |
---|
| 424 | # every parent key. See the @ref up and @ref down methods for more |
---|
| 425 | # information. |
---|
[205] | 426 | class HiveIterator(): |
---|
| 427 | hive = None |
---|
| 428 | iter = None |
---|
[206] | 429 | iteration_root = None |
---|
[205] | 430 | |
---|
| 431 | def __init__(self, hive): |
---|
[213] | 432 | self.iter = regfi.regfi_iterator_new(hive.file, REGFI_ENCODING_UTF8) |
---|
[205] | 433 | if self.iter == None: |
---|
| 434 | raise Exception("Could not create iterator. Current log:\n" |
---|
| 435 | + GetLogMessages()) |
---|
[214] | 436 | self._hive = hive |
---|
[205] | 437 | |
---|
| 438 | def __getattr__(self, name): |
---|
| 439 | return getattr(self.file.contents, name) |
---|
| 440 | |
---|
| 441 | def __del__(self): |
---|
[213] | 442 | regfi.regfi_iterator_free(self.iter) |
---|
[205] | 443 | |
---|
| 444 | def __iter__(self): |
---|
[206] | 445 | self.iteration_root = None |
---|
[205] | 446 | return self |
---|
| 447 | |
---|
| 448 | def __next__(self): |
---|
[206] | 449 | if self.iteration_root == None: |
---|
[213] | 450 | self.iteration_root = self.current_key() |
---|
[205] | 451 | elif not regfi.regfi_iterator_down(self.iter): |
---|
| 452 | up_ret = regfi.regfi_iterator_up(self.iter) |
---|
[206] | 453 | while (up_ret and |
---|
| 454 | not regfi.regfi_iterator_next_subkey(self.iter)): |
---|
| 455 | if self.iteration_root == self.current_key(): |
---|
| 456 | self.iteration_root = None |
---|
| 457 | raise StopIteration('') |
---|
[205] | 458 | up_ret = regfi.regfi_iterator_up(self.iter) |
---|
| 459 | |
---|
| 460 | if not up_ret: |
---|
| 461 | raise StopIteration('') |
---|
| 462 | |
---|
[210] | 463 | # XXX: Use non-generic exception |
---|
[205] | 464 | if not regfi.regfi_iterator_down(self.iter): |
---|
| 465 | raise Exception('Error traversing iterator downward.'+ |
---|
| 466 | ' Current log:\n'+ GetLogMessages()) |
---|
| 467 | |
---|
| 468 | regfi.regfi_iterator_first_subkey(self.iter) |
---|
[206] | 469 | return self.current_key() |
---|
[205] | 470 | |
---|
[212] | 471 | # For Python 2.x |
---|
[214] | 472 | next = __next__ |
---|
[212] | 473 | |
---|
[206] | 474 | def down(self): |
---|
| 475 | pass |
---|
| 476 | |
---|
| 477 | def up(self): |
---|
| 478 | pass |
---|
| 479 | |
---|
| 480 | def descend(self, path): |
---|
| 481 | #set up generator |
---|
| 482 | cpath = (bytes(p,'ascii') for p in path) |
---|
| 483 | |
---|
| 484 | # evaluate generator and create char* array |
---|
| 485 | apath = (c_char_p*len(path))(*cpath) |
---|
| 486 | |
---|
[210] | 487 | # XXX: Use non-generic exception |
---|
[206] | 488 | if not regfi.regfi_iterator_walk_path(self.iter,apath): |
---|
| 489 | raise Exception('Could not locate path.\n'+GetLogMessages()) |
---|
| 490 | |
---|
| 491 | def current_key(self): |
---|
[214] | 492 | return Key(self._hive, regfi.regfi_iterator_cur_key(self.iter)) |
---|
[210] | 493 | |
---|
| 494 | #XXX Add subkey/value search accessor functions (?) |
---|