Changeset 220
- Timestamp:
- 03/31/11 13:20:05 (14 years ago)
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
test/pyregfi-smoketest.py
r219 r220 8 8 def usage(): 9 9 sys.stderr.write("USAGE: pyregfi-smoketest.py hive1 [hive2 ...]\n") 10 11 12 13 14 # Uses the HiveIterator to walk all keys 15 # Gathers various (meaningless) statistics to exercise simple attribute access 16 # and to hopefully smoke out any bugs that can be identified by changing stats 17 def iterTallyNames(hive): 18 key_count = 0 19 key_lens = 0 20 key_rawlens = 0 21 value_count = 0 22 value_lens = 0 23 value_rawlens = 0 24 25 for k in hive: 26 key_count += 1 27 if k.name != None: 28 key_lens += len(k.name) 29 if k.name_raw != None: 30 key_rawlens += len(k.name_raw) 31 32 for v in k.values: 33 value_count += 1 34 if v.name != None: 35 value_lens += len(v.name) 36 if v.name_raw != None: 37 value_rawlens += len(v.name_raw) 38 39 print(" Counts: keys=%d, values=%d" % (key_count, value_count)) 40 print(" Total name length: keys=%d, values=%d" % (key_lens, value_lens)) 41 print(" Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens)) 10 42 11 43 … … 23 55 24 56 return path 25 26 27 # Uses the HiveIterator to walk all keys28 # Gathers various (meaningless) statistics to exercise simple attribute access29 # and to hopefully smoke out any bugs that can be identified by changing stats30 def iterTallyNames(hive):31 key_count = 032 key_lens = 033 key_rawlens = 034 value_count = 035 value_lens = 036 value_rawlens = 037 38 for k in hive:39 key_count += 140 if k.name != None:41 key_lens += len(k.name)42 if k.name_raw != None:43 key_rawlens += len(k.name_raw)44 45 for v in k.values:46 value_count += 147 if v.name != None:48 value_lens += len(v.name)49 if v.name_raw != None:50 value_rawlens += len(v.name_raw)51 52 print(" Counts: keys=%d, values=%d" % (key_count, value_count))53 print(" Total name length: keys=%d, values=%d" % (key_lens, value_lens))54 print(" Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens))55 56 57 57 58 # For each key in the hive, this traverses the parent links up to the root, … … 154 155 print(" Modified stat: %f" % modified_stat) 155 156 157 158 159 def iterIterWalk(hive): 160 sk_stat = 0.0 161 v_stat = 0.0 162 iter = pyregfi.HiveIterator(hive) 163 for k in iter: 164 path = getCurrentPath(k) 165 try: 166 hive_iter = hive.subtree(path) 167 sk = hive_iter.first_subkey() 168 while sk != None: 169 ssk = hive_iter.find_subkey(sk.name) 170 sk_stat += len(ssk.name) 171 sk = hive_iter.next_subkey() 172 173 v = hive_iter.first_value() 174 while v != None: 175 vv = hive_iter.find_value(v.name) 176 v_stat += len(vv.name) 177 v = hive_iter.next_value() 178 179 except Exception as e: 180 print("WARNING: Could not decend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e)) 181 print(" Subkey stat: %f" % sk_stat) 182 print(" Value stat: %f" % v_stat) 183 184 185 156 186 if len(sys.argv) < 2: 157 187 usage() … … 159 189 160 190 161 #tests = [("iterTallyNames",iterTallyNames),("iterParentWalk",iterParentWalk),("iterTallyData",iterTallyData),("recurseKeyTally",recurseKeyTally),("iterFetchRelated",iterFetchRelated),] 162 tests = [("iterFetchRelated",iterFetchRelated),] 191 tests = [("iterTallyNames",iterTallyNames), 192 ("iterParentWalk",iterParentWalk), 193 ("iterTallyData",iterTallyData), 194 ("recurseKeyTally",recurseKeyTally), 195 ("iterFetchRelated",iterFetchRelated), 196 ("iterIterWalk",iterIterWalk),] 197 198 tests = [("iterIterWalk",iterIterWalk),] 199 163 200 164 201 files = [] -
trunk/python/pyregfi/__init__.py
r219 r220 12 12 import ctypes 13 13 import ctypes.util 14 from ctypes import c_char,c_char_p,c_int,c_uint16,c_uint32,c_bool,POINTER15 16 regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True)17 18 19 regfi.regfi_alloc.argtypes = [c_int, REGFI_ENCODING]20 regfi.regfi_alloc.restype = POINTER(REGFI_FILE)21 22 regfi.regfi_alloc_cb.argtypes = [POINTER(REGFI_RAW_FILE), REGFI_ENCODING]23 regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE)24 25 regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)]26 regfi.regfi_free.restype = None27 28 regfi.regfi_log_get_str.argtypes = []29 regfi.regfi_log_get_str.restype = c_char_p30 31 regfi.regfi_log_set_mask.argtypes = [c_uint16]32 regfi.regfi_log_set_mask.restype = c_bool33 34 regfi.regfi_get_rootkey.argtypes = [POINTER(REGFI_FILE)]35 regfi.regfi_get_rootkey.restype = POINTER(REGFI_NK)36 37 regfi.regfi_free_record.argtypes = [c_void_p]38 regfi.regfi_free_record.restype = None39 40 regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)]41 regfi.regfi_fetch_num_subkeys.restype = c_uint3242 43 regfi.regfi_fetch_num_values.argtypes = [POINTER(REGFI_NK)]44 regfi.regfi_fetch_num_values.restype = c_uint3245 46 regfi.regfi_fetch_classname.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]47 regfi.regfi_fetch_classname.restype = POINTER(REGFI_CLASSNAME)48 49 regfi.regfi_fetch_sk.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]50 regfi.regfi_fetch_sk.restype = POINTER(REGFI_SK)51 52 regfi.regfi_fetch_data.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_VK)]53 regfi.regfi_fetch_data.restype = POINTER(REGFI_DATA)54 55 regfi.regfi_find_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),56 c_char_p, POINTER(c_uint32)]57 regfi.regfi_find_subkey.restype = c_bool58 59 regfi.regfi_find_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),60 c_char_p, POINTER(c_uint32)]61 regfi.regfi_find_value.restype = c_bool62 63 regfi.regfi_get_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),64 c_uint32]65 regfi.regfi_get_subkey.restype = POINTER(REGFI_NK)66 67 regfi.regfi_get_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),68 c_uint32]69 regfi.regfi_get_value.restype = POINTER(REGFI_VK)70 71 regfi.regfi_get_parentkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]72 regfi.regfi_get_parentkey.restype = POINTER(REGFI_NK)73 74 regfi.regfi_nt2unix_time.argtypes = [POINTER(REGFI_NTTIME)]75 regfi.regfi_nt2unix_time.restype = c_double76 77 regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING]78 regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR)79 80 regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)]81 regfi.regfi_iterator_free.restype = None82 83 regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)]84 regfi.regfi_iterator_down.restype = c_bool85 86 regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)]87 regfi.regfi_iterator_up.restype = c_bool88 89 regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)]90 regfi.regfi_iterator_to_root.restype = c_bool91 92 regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)]93 regfi.regfi_iterator_walk_path.restype = c_bool94 95 regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)]96 regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK)97 98 regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)]99 regfi.regfi_iterator_first_subkey.restype = c_bool100 101 regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)]102 regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK)103 104 regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)]105 regfi.regfi_iterator_next_subkey.restype = c_bool106 107 regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]108 regfi.regfi_iterator_find_subkey.restype = c_bool109 110 regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)]111 regfi.regfi_iterator_first_value.restype = c_bool112 113 regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)]114 regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK)115 116 regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)]117 regfi.regfi_iterator_next_value.restype = c_bool118 119 regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]120 regfi.regfi_iterator_find_value.restype = c_bool121 122 123 regfi.regfi_init.argtypes = []124 regfi.regfi_init.restype = None125 regfi.regfi_init()126 14 127 15 … … 151 39 ret_val.append(s.encode('utf-8', 'replace')) 152 40 153 ret_val = (c _char_p*(len(str_list)+1))(*ret_val)41 ret_val = (ctypes.c_char_p*(len(str_list)+1))(*ret_val) 154 42 # Terminate the char** with a NULL pointer 155 43 ret_val[-1] = 0 … … 246 134 247 135 def __getitem__(self, name): 248 index = c _uint32()136 index = ctypes.c_uint32() 249 137 if isinstance(name, str): 250 138 name = name.encode('utf-8') … … 253 141 name = create_string_buffer(bytes(name)) 254 142 255 if self._find_element(self._hive.file, self._key._base, name, byref(index)): 256 return self._constructor(self._hive, 143 if self._find_element(self._hive.file, self._key._base, 144 name, byref(index)): 145 return self._constructor(self._hive, 257 146 self._get_element(self._hive.file, 258 147 self._key._base, … … 275 164 276 165 elem = self._get_element(self._hive.file, self._key._base, 277 c _uint32(self._current))166 ctypes.c_uint32(self._current)) 278 167 self._current += 1 279 168 return self._constructor(self._hive, elem) … … 493 382 # information. 494 383 class HiveIterator(): 495 hive = None496 iter = None497 iteration_root = None384 _hive = None 385 _iter = None 386 _iteration_root = None 498 387 499 388 def __init__(self, hive): 500 self. iter = regfi.regfi_iterator_new(hive.file, REGFI_ENCODING_UTF8)501 if self. iter == None:389 self._iter = regfi.regfi_iterator_new(hive.file, REGFI_ENCODING_UTF8) 390 if self._iter == None: 502 391 raise Exception("Could not create iterator. Current log:\n" 503 392 + GetLogMessages()) … … 508 397 509 398 def __del__(self): 510 regfi.regfi_iterator_free(self. iter)399 regfi.regfi_iterator_free(self._iter) 511 400 512 401 def __iter__(self): 513 self. iteration_root = None402 self._iteration_root = None 514 403 return self 515 404 516 405 def __next__(self): 517 if self. iteration_root == None:518 self. iteration_root = self.current_key()519 elif not regfi.regfi_iterator_down(self. iter):520 up_ret = regfi.regfi_iterator_up(self. iter)406 if self._iteration_root == None: 407 self._iteration_root = self.current_key() 408 elif not regfi.regfi_iterator_down(self._iter): 409 up_ret = regfi.regfi_iterator_up(self._iter) 521 410 while (up_ret and 522 not regfi.regfi_iterator_next_subkey(self. iter)):523 if self. iteration_root == self.current_key():524 self. iteration_root = None411 not regfi.regfi_iterator_next_subkey(self._iter)): 412 if self._iteration_root == self.current_key(): 413 self._iteration_root = None 525 414 raise StopIteration('') 526 up_ret = regfi.regfi_iterator_up(self. iter)415 up_ret = regfi.regfi_iterator_up(self._iter) 527 416 528 417 if not up_ret: … … 530 419 531 420 # XXX: Use non-generic exception 532 if not regfi.regfi_iterator_down(self. iter):421 if not regfi.regfi_iterator_down(self._iter): 533 422 raise Exception('Error traversing iterator downward.'+ 534 423 ' Current log:\n'+ GetLogMessages()) 535 424 536 regfi.regfi_iterator_first_subkey(self. iter)425 regfi.regfi_iterator_first_subkey(self._iter) 537 426 return self.current_key() 538 427 … … 540 429 next = __next__ 541 430 542 def down(self): 543 pass 431 def down(self, subkey_name=None): 432 if subkey_name == None: 433 return regfi.regfi_iterator_down(self._iter) 434 else: 435 if name != None: 436 name = name.encode('utf-8') 437 return (regfi.regfi_iterator_find_subkey(self._iter, name) 438 and regfi.regfi_iterator_down(self._iter)) 544 439 545 440 def up(self): 546 pass 441 return regfi.regfi_iterator_up(self._iter) 442 443 def first_subkey(self): 444 if regfi.regfi_iterator_first_subkey(self._iter): 445 return self.current_subkey() 446 return None 447 448 def first_value(self): 449 if regfi.regfi_iterator_first_value(self._iter): 450 return self.current_value() 451 return None 452 453 def next_subkey(self): 454 if regfi.regfi_iterator_next_subkey(self._iter): 455 return self.current_subkey() 456 return None 457 458 def next_value(self): 459 if regfi.regfi_iterator_next_value(self._iter): 460 return self.current_value() 461 return None 462 463 def find_subkey(self, name): 464 if name != None: 465 name = name.encode('utf-8') 466 if regfi.regfi_iterator_find_subkey(self._iter, name): 467 return self.current_subkey() 468 return None 469 470 def find_value(self, name): 471 if name != None: 472 name = name.encode('utf-8') 473 if regfi.regfi_iterator_find_value(self._iter, name): 474 return self.current_value() 475 return None 476 477 def current_subkey(self): 478 return Key(self._hive, regfi.regfi_iterator_cur_subkey(self._iter)) 479 480 def current_value(self): 481 return Value(self._hive, regfi.regfi_iterator_cur_value(self._iter)) 482 483 def current_key(self): 484 return Key(self._hive, regfi.regfi_iterator_cur_key(self._iter)) 547 485 548 486 def descend(self, path): … … 550 488 551 489 # XXX: Use non-generic exception 552 if not regfi.regfi_iterator_walk_path(self. iter, cpath):490 if not regfi.regfi_iterator_walk_path(self._iter, cpath): 553 491 raise Exception('Could not locate path.\n'+GetLogMessages()) 554 555 def current_key(self):556 return Key(self._hive, regfi.regfi_iterator_cur_key(self.iter))557 558 #XXX Add subkey/value search accessor functions (?) -
trunk/python/pyregfi/structures.py
r215 r220 224 224 ('state', c_void_p), 225 225 ] 226 227 228 # Load libregfi and define function prototypes 229 regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True) 230 231 regfi.regfi_alloc.argtypes = [c_int, REGFI_ENCODING] 232 regfi.regfi_alloc.restype = POINTER(REGFI_FILE) 233 234 regfi.regfi_alloc_cb.argtypes = [POINTER(REGFI_RAW_FILE), REGFI_ENCODING] 235 regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE) 236 237 regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)] 238 regfi.regfi_free.restype = None 239 240 regfi.regfi_log_get_str.argtypes = [] 241 regfi.regfi_log_get_str.restype = c_char_p 242 243 regfi.regfi_log_set_mask.argtypes = [c_uint16] 244 regfi.regfi_log_set_mask.restype = c_bool 245 246 regfi.regfi_get_rootkey.argtypes = [POINTER(REGFI_FILE)] 247 regfi.regfi_get_rootkey.restype = POINTER(REGFI_NK) 248 249 regfi.regfi_free_record.argtypes = [c_void_p] 250 regfi.regfi_free_record.restype = None 251 252 regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)] 253 regfi.regfi_fetch_num_subkeys.restype = c_uint32 254 255 regfi.regfi_fetch_num_values.argtypes = [POINTER(REGFI_NK)] 256 regfi.regfi_fetch_num_values.restype = c_uint32 257 258 regfi.regfi_fetch_classname.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)] 259 regfi.regfi_fetch_classname.restype = POINTER(REGFI_CLASSNAME) 260 261 regfi.regfi_fetch_sk.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)] 262 regfi.regfi_fetch_sk.restype = POINTER(REGFI_SK) 263 264 regfi.regfi_fetch_data.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_VK)] 265 regfi.regfi_fetch_data.restype = POINTER(REGFI_DATA) 266 267 regfi.regfi_find_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), 268 c_char_p, POINTER(c_uint32)] 269 regfi.regfi_find_subkey.restype = c_bool 270 271 regfi.regfi_find_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), 272 c_char_p, POINTER(c_uint32)] 273 regfi.regfi_find_value.restype = c_bool 274 275 regfi.regfi_get_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), 276 c_uint32] 277 regfi.regfi_get_subkey.restype = POINTER(REGFI_NK) 278 279 regfi.regfi_get_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK), 280 c_uint32] 281 regfi.regfi_get_value.restype = POINTER(REGFI_VK) 282 283 regfi.regfi_get_parentkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)] 284 regfi.regfi_get_parentkey.restype = POINTER(REGFI_NK) 285 286 regfi.regfi_nt2unix_time.argtypes = [POINTER(REGFI_NTTIME)] 287 regfi.regfi_nt2unix_time.restype = c_double 288 289 regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING] 290 regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR) 291 292 regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)] 293 regfi.regfi_iterator_free.restype = None 294 295 regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)] 296 regfi.regfi_iterator_down.restype = c_bool 297 298 regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)] 299 regfi.regfi_iterator_up.restype = c_bool 300 301 regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)] 302 regfi.regfi_iterator_to_root.restype = c_bool 303 304 regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)] 305 regfi.regfi_iterator_walk_path.restype = c_bool 306 307 regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)] 308 regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK) 309 310 regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)] 311 regfi.regfi_iterator_first_subkey.restype = c_bool 312 313 regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)] 314 regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK) 315 316 regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)] 317 regfi.regfi_iterator_next_subkey.restype = c_bool 318 319 regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] 320 regfi.regfi_iterator_find_subkey.restype = c_bool 321 322 regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)] 323 regfi.regfi_iterator_first_value.restype = c_bool 324 325 regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)] 326 regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK) 327 328 regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)] 329 regfi.regfi_iterator_next_value.restype = c_bool 330 331 regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] 332 regfi.regfi_iterator_find_value.restype = c_bool 333 334 335 regfi.regfi_init.argtypes = [] 336 regfi.regfi_init.restype = None 337 regfi.regfi_init()
Note: See TracChangeset
for help on using the changeset viewer.