Ignore:
Timestamp:
08/23/10 11:51:56 (14 years ago)
Author:
tim
Message:

wrapper data structures and part of iterator interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/pyregfi/__init__.py

    r204 r205  
    66import ctypes
    77import ctypes.util
    8 from ctypes import c_char,c_char_p,c_int,POINTER
     8from ctypes import c_char,c_char_p,c_int,c_uint16,c_bool,POINTER
    99
    1010regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True)
     
    1717regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE)
    1818
     19regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)]
     20regfi.regfi_free.restype = None
     21
    1922regfi.regfi_log_get_str.argtypes = []
    2023regfi.regfi_log_get_str.restype = c_char_p
     24
     25regfi.regfi_log_set_mask.argtypes = [c_uint16]
     26regfi.regfi_log_set_mask.restype = c_bool
     27
     28regfi.regfi_free_record.argtypes = [c_void_p]
     29regfi.regfi_free_record.restype = None
     30
     31regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING]
     32regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR)
     33
     34regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)]
     35regfi.regfi_iterator_free.restype = None
     36
     37regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)]
     38regfi.regfi_iterator_down.restype = c_bool
     39
     40regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)]
     41regfi.regfi_iterator_up.restype = c_bool
     42
     43regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)]
     44regfi.regfi_iterator_to_root.restype = c_bool
     45
     46regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)]
     47regfi.regfi_iterator_walk_path.restype = c_bool
     48
     49regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)]
     50regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK)
     51
     52regfi.regfi_iterator_cur_sk.argtypes = [POINTER(REGFI_ITERATOR)]
     53regfi.regfi_iterator_cur_sk.restype = POINTER(REGFI_SK)
     54
     55regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
     56regfi.regfi_iterator_first_subkey.restype = c_bool
     57
     58regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
     59regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK)
     60
     61regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
     62regfi.regfi_iterator_next_subkey.restype = c_bool
     63
     64regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
     65regfi.regfi_iterator_find_subkey.restype = c_bool
     66
     67regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)]
     68regfi.regfi_iterator_first_value.restype = c_bool
     69
     70regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)]
     71regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK)
     72
     73regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)]
     74regfi.regfi_iterator_next_value.restype = c_bool
     75
     76regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
     77regfi.regfi_iterator_find_value.restype = c_bool
     78
     79# XXX: possibly move REGFI_ENCODING to file object and eliminate need for ITERATOR here.
     80regfi.regfi_iterator_fetch_classname.argtypes = [POINTER(REGFI_ITERATOR), POINTER(REGFI_NK)]
     81regfi.regfi_iterator_fetch_classname.restype = POINTER(REGFI_CLASSNAME)
     82
     83regfi.regfi_iterator_fetch_data.argtypes = [POINTER(REGFI_ITERATOR), POINTER(REGFI_VK)]
     84regfi.regfi_iterator_fetch_data.restype = POINTER(REGFI_DATA)
     85
    2186
    2287regfi.regfi_init.argtypes = []
     
    2590
    2691
    27 class Hive(ctypes.Structure):
    28    
     92def GetLogMessages():
     93    return regfi.regfi_log_get_str()
     94
     95
     96class Hive():   
    2997    file = None
    3098    raw_file = None
    3199   
    32100    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
    33109       
    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)
    43115
    44116    def __getattr__(self, name):
    45117        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           
    46124
    47     def test(self):
    48         print(self.magic)
    49         print(self.sequence1)
    50         print(self.sequence2)
     125    def __iter__(self):
     126        return HiveIterator(self)
    51127
     128
     129class 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.