Ignore:
Timestamp:
08/25/10 11:20:32 (14 years ago)
Author:
tim
Message:

simplified part of regfi API to move string encoding to the REGFI_FILE object

additional pyregfi implementation

File:
1 edited

Legend:

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

    r205 r206  
    2929regfi.regfi_free_record.restype = None
    3030
     31regfi.regfi_fetch_classname.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]
     32regfi.regfi_fetch_classname.restype = POINTER(REGFI_CLASSNAME)
     33
     34regfi.regfi_fetch_sk.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]
     35regfi.regfi_fetch_sk.restype = POINTER(REGFI_SK)
     36
     37regfi.regfi_fetch_data.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_VK)]
     38regfi.regfi_fetch_data.restype = POINTER(REGFI_DATA)
     39
    3140regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING]
    3241regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR)
     
    5059regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK)
    5160
    52 regfi.regfi_iterator_cur_sk.argtypes = [POINTER(REGFI_ITERATOR)]
    53 regfi.regfi_iterator_cur_sk.restype = POINTER(REGFI_SK)
    54 
    5561regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
    5662regfi.regfi_iterator_first_subkey.restype = c_bool
     
    7682regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
    7783regfi.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)
    8584
    8685
     
    9190
    9291def GetLogMessages():
    93     return regfi.regfi_log_get_str()
    94 
     92    msgs = regfi.regfi_log_get_str()
     93    if msgs == None:
     94        return ''
     95    return msgs
     96
     97
     98class _StructureWrapper():
     99    "Handles memory management and proxies attribute access to base structures"
     100    base = None
     101
     102    def __init__(self, base):
     103        # XXX: check for NULL here, throw an exception if so.
     104        self.base = base
     105
     106    def __del__(self):
     107        regfi.regfi_free_record(self.base)
     108
     109    def __getattr__(self, name):
     110        return getattr(self.base.contents, name)
     111
     112    def __eq__(self, other):
     113        return (type(self) == type(other)) and (self.offset == other.offset)
     114
     115    def __ne__(self, other):
     116        return (not self.__eq__(other))
     117
     118
     119class Key(_StructureWrapper):
     120    pass
     121
     122class Value(_StructureWrapper):
     123    pass
     124
     125class Data(_StructureWrapper):
     126    pass
     127
     128class Security(_StructureWrapper):
     129    pass
    95130
    96131class Hive():   
     
    126161        return HiveIterator(self)
    127162
     163    def subtree(self, path):
     164        hi = HiveIterator(self)
     165        hi.descend(path)
     166        return hi
     167
    128168
    129169class HiveIterator():
    130170    hive = None
    131171    iter = None
    132     root_traversed = False
     172    iteration_root = None
    133173
    134174    def __init__(self, hive):
     
    147187
    148188    def __iter__(self):
     189        self.iteration_root = None
    149190        return self
    150191
    151192    def __next__(self):
    152         if self.root_traversed:
    153             self.root_traversed = True
    154            
     193        if self.iteration_root == None:
     194            self.iteration_root = self.current_key()           
    155195        elif not regfi.regfi_iterator_down(self.iter):
    156196            up_ret = regfi.regfi_iterator_up(self.iter)
    157             while up_ret and not regfi.regfi_iterator_next_subkey(self.iter):
     197            while (up_ret and
     198                   not regfi.regfi_iterator_next_subkey(self.iter)):
     199                if self.iteration_root == self.current_key():
     200                    self.iteration_root = None
     201                    raise StopIteration('')
    158202                up_ret = regfi.regfi_iterator_up(self.iter)
    159203
     
    166210
    167211        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 
     212        return self.current_key()
     213
     214    def down(self):
     215        pass
     216
     217    def up(self):
     218        pass
     219
     220    def descend(self, path):
     221        #set up generator
     222        cpath = (bytes(p,'ascii') for p in path)
     223
     224        # evaluate generator and create char* array
     225        apath = (c_char_p*len(path))(*cpath)
     226
     227        if not regfi.regfi_iterator_walk_path(self.iter,apath):
     228            raise Exception('Could not locate path.\n'+GetLogMessages())
     229
     230    def current_key(self):
     231        return Key(regfi.regfi_iterator_cur_key(self.iter))
Note: See TracChangeset for help on using the changeset viewer.