Ignore:
Timestamp:
09/06/10 23:03:36 (14 years ago)
Author:
tim
Message:

reworked regfi API to accomodate more convenient subkey/value searches without an iterator.

continued implementation on pyregfi

File:
1 edited

Legend:

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

    r206 r207  
    66import ctypes
    77import ctypes.util
    8 from ctypes import c_char,c_char_p,c_int,c_uint16,c_bool,POINTER
     8from ctypes import c_char,c_char_p,c_int,c_uint16,c_uint32,c_bool,POINTER
    99
    1010regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True)
     
    2929regfi.regfi_free_record.restype = None
    3030
     31regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)]
     32regfi.regfi_fetch_num_subkeys.restype = c_uint32
     33
     34regfi.regfi_fetch_num_values.argtypes = [POINTER(REGFI_NK)]
     35regfi.regfi_fetch_num_values.restype = c_uint32
     36
    3137regfi.regfi_fetch_classname.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]
    3238regfi.regfi_fetch_classname.restype = POINTER(REGFI_CLASSNAME)
     
    3844regfi.regfi_fetch_data.restype = POINTER(REGFI_DATA)
    3945
     46regfi.regfi_find_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     47                                    c_char_p, POINTER(c_uint32)]
     48regfi.regfi_find_subkey.restype = c_bool
     49
     50regfi.regfi_find_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     51                                    c_char_p, POINTER(c_uint32)]
     52regfi.regfi_find_value.restype = c_bool
     53
     54regfi.regfi_get_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     55                                   c_uint32]
     56regfi.regfi_get_subkey.restype = POINTER(REGFI_NK)
     57
     58regfi.regfi_get_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     59                                   c_uint32]
     60regfi.regfi_get_value.restype = POINTER(REGFI_VK)
     61
    4062regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING]
    4163regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR)
     
    83105regfi.regfi_iterator_find_value.restype = c_bool
    84106
     107
     108
     109regfi.regfi_get_value
    85110
    86111regfi.regfi_init.argtypes = []
     
    93118    if msgs == None:
    94119        return ''
    95     return msgs
     120    return msgs.decode('ascii')
    96121
    97122
    98123class _StructureWrapper():
    99124    "Handles memory management and proxies attribute access to base structures"
     125    hive = None
    100126    base = None
    101127
    102     def __init__(self, base):
     128    def __init__(self, hive, base):
     129        self.hive = hive
    103130        # XXX: check for NULL here, throw an exception if so.
    104131        self.base = base
     
    117144
    118145
    119 class Key(_StructureWrapper):
    120     pass
    121 
    122146class Value(_StructureWrapper):
    123147    pass
    124148
     149
    125150class Data(_StructureWrapper):
    126151    pass
    127152
     153
    128154class Security(_StructureWrapper):
    129155    pass
     156
     157
     158class _ValueList():
     159    hive = None
     160    key = None
     161    length = None
     162    current = None
     163
     164    def __init__(self, key):
     165        self.hive = key.hive
     166        # XXX: check for NULL here, throw an exception if so.
     167        self.key = key
     168        self.length = regfi.regfi_fetch_num_values(key.base)
     169   
     170    def __len__(self):
     171        return self.length
     172
     173    def __getitem__(self, name):
     174        index = c_uint32()
     175        # XXX: need to do any funky unicode conversions on name?
     176        if regfi.regfi_find_value(self.hive.file, self.key.base,
     177                                  create_string_buffer(name), byref(index)):
     178            return Value(hive,
     179                         regfi.regfi_get_value(hive.file, key.base, index))
     180        raise KeyError('')
     181
     182    def __iter__(self):
     183        self.current = 0
     184        return self
     185   
     186    def __next__(self):
     187        if self.current >= self.length:
     188            raise StopIteration('')
     189
     190        vk = regfi.regfi_get_value(self.hive.file, self.key.base,
     191                                   c_uint32(self.current))
     192        self.current += 1
     193        return vk.contents
     194   
     195
     196class Key(_StructureWrapper):
     197    values = None
     198
     199    def __init__(self, hive, base):
     200        super(Key, self).__init__(hive, base)
     201        self.values = _ValueList(self)
     202
     203    def fetch_security(self):
     204        return Security(self.hive,
     205                        regfi.regfi_fetch_sk(self.hive.file, self.base))
     206
    130207
    131208class Hive():   
     
    229306
    230307    def current_key(self):
    231         return Key(regfi.regfi_iterator_cur_key(self.iter))
     308        return Key(self.hive, regfi.regfi_iterator_cur_key(self.iter))
Note: See TracChangeset for help on using the changeset viewer.