Changeset 205 for trunk/python/pyregfi/__init__.py
- Timestamp:
- 08/23/10 11:51:56 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/pyregfi/__init__.py
r204 r205 6 6 import ctypes 7 7 import ctypes.util 8 from ctypes import c_char,c_char_p,c_int, POINTER8 from ctypes import c_char,c_char_p,c_int,c_uint16,c_bool,POINTER 9 9 10 10 regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True) … … 17 17 regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE) 18 18 19 regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)] 20 regfi.regfi_free.restype = None 21 19 22 regfi.regfi_log_get_str.argtypes = [] 20 23 regfi.regfi_log_get_str.restype = c_char_p 24 25 regfi.regfi_log_set_mask.argtypes = [c_uint16] 26 regfi.regfi_log_set_mask.restype = c_bool 27 28 regfi.regfi_free_record.argtypes = [c_void_p] 29 regfi.regfi_free_record.restype = None 30 31 regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING] 32 regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR) 33 34 regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)] 35 regfi.regfi_iterator_free.restype = None 36 37 regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)] 38 regfi.regfi_iterator_down.restype = c_bool 39 40 regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)] 41 regfi.regfi_iterator_up.restype = c_bool 42 43 regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)] 44 regfi.regfi_iterator_to_root.restype = c_bool 45 46 regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)] 47 regfi.regfi_iterator_walk_path.restype = c_bool 48 49 regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)] 50 regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK) 51 52 regfi.regfi_iterator_cur_sk.argtypes = [POINTER(REGFI_ITERATOR)] 53 regfi.regfi_iterator_cur_sk.restype = POINTER(REGFI_SK) 54 55 regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)] 56 regfi.regfi_iterator_first_subkey.restype = c_bool 57 58 regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)] 59 regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK) 60 61 regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)] 62 regfi.regfi_iterator_next_subkey.restype = c_bool 63 64 regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] 65 regfi.regfi_iterator_find_subkey.restype = c_bool 66 67 regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)] 68 regfi.regfi_iterator_first_value.restype = c_bool 69 70 regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)] 71 regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK) 72 73 regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)] 74 regfi.regfi_iterator_next_value.restype = c_bool 75 76 regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] 77 regfi.regfi_iterator_find_value.restype = c_bool 78 79 # XXX: possibly move REGFI_ENCODING to file object and eliminate need for ITERATOR here. 80 regfi.regfi_iterator_fetch_classname.argtypes = [POINTER(REGFI_ITERATOR), POINTER(REGFI_NK)] 81 regfi.regfi_iterator_fetch_classname.restype = POINTER(REGFI_CLASSNAME) 82 83 regfi.regfi_iterator_fetch_data.argtypes = [POINTER(REGFI_ITERATOR), POINTER(REGFI_VK)] 84 regfi.regfi_iterator_fetch_data.restype = POINTER(REGFI_DATA) 85 21 86 22 87 regfi.regfi_init.argtypes = [] … … 25 90 26 91 27 class Hive(ctypes.Structure): 28 92 def GetLogMessages(): 93 return regfi.regfi_log_get_str() 94 95 96 class Hive(): 29 97 file = None 30 98 raw_file = None 31 99 32 100 def __init__(self, fh): 101 # The fileno method may not exist, or it may thrown an exception 102 # when called if the file isn't backed with a descriptor. 103 try: 104 if hasattr(fh, 'fileno'): 105 self.file = regfi.regfi_alloc(fh.fileno()) 106 return 107 except: 108 pass 33 109 34 if hasattr(fh, 'fileno') and 1==0: 35 self.file = regfi.regfi_alloc(fh.fileno()) 36 else: 37 self.raw_file = structures.REGFI_RAW_FILE() 38 self.raw_file.fh = fh 39 self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek) 40 self.raw_file.read = read_cb_type(self.raw_file.cb_read) 41 self.file = regfi.regfi_alloc_cb(self.raw_file) 42 print(regfi.regfi_log_get_str()) 110 self.raw_file = structures.REGFI_RAW_FILE() 111 self.raw_file.fh = fh 112 self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek) 113 self.raw_file.read = read_cb_type(self.raw_file.cb_read) 114 self.file = regfi.regfi_alloc_cb(self.raw_file) 43 115 44 116 def __getattr__(self, name): 45 117 return getattr(self.file.contents, name) 118 119 def __del__(self): 120 regfi.regfi_free(self.file) 121 if self.raw_file != None: 122 regfi.regfi_free(self.file) 123 46 124 47 def test(self): 48 print(self.magic) 49 print(self.sequence1) 50 print(self.sequence2) 125 def __iter__(self): 126 return HiveIterator(self) 51 127 128 129 class HiveIterator(): 130 hive = None 131 iter = None 132 root_traversed = False 133 134 def __init__(self, hive): 135 # REGFI_ENCODING_UTF8==1 136 self.iter = regfi.regfi_iterator_new(hive.file, 1) 137 if self.iter == None: 138 raise Exception("Could not create iterator. Current log:\n" 139 + GetLogMessages()) 140 self.hive = hive 141 142 def __getattr__(self, name): 143 return getattr(self.file.contents, name) 144 145 def __del__(self): 146 regfi.regfi_iterator_free(self.iter) 147 148 def __iter__(self): 149 return self 150 151 def __next__(self): 152 if self.root_traversed: 153 self.root_traversed = True 154 155 elif not regfi.regfi_iterator_down(self.iter): 156 up_ret = regfi.regfi_iterator_up(self.iter) 157 while up_ret and not regfi.regfi_iterator_next_subkey(self.iter): 158 up_ret = regfi.regfi_iterator_up(self.iter) 159 160 if not up_ret: 161 raise StopIteration('') 162 163 if not regfi.regfi_iterator_down(self.iter): 164 raise Exception('Error traversing iterator downward.'+ 165 ' Current log:\n'+ GetLogMessages()) 166 167 regfi.regfi_iterator_first_subkey(self.iter) 168 print(regfi.regfi_iterator_cur_key(self.iter).contents.keyname) 169 return regfi.regfi_iterator_cur_key(self.iter) 170
Note: See TracChangeset
for help on using the changeset viewer.