Changeset 220 for trunk


Ignore:
Timestamp:
03/31/11 13:20:05 (13 years ago)
Author:
tim
Message:

reorganized code
added remaining methods to iterator
added test case for new methods

Location:
trunk/python/pyregfi
Files:
2 edited

Legend:

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

    r219 r220  
    1212import ctypes
    1313import ctypes.util
    14 from ctypes import c_char,c_char_p,c_int,c_uint16,c_uint32,c_bool,POINTER
    15 
    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 = None
    27 
    28 regfi.regfi_log_get_str.argtypes = []
    29 regfi.regfi_log_get_str.restype = c_char_p
    30 
    31 regfi.regfi_log_set_mask.argtypes = [c_uint16]
    32 regfi.regfi_log_set_mask.restype = c_bool
    33 
    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 = None
    39 
    40 regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)]
    41 regfi.regfi_fetch_num_subkeys.restype = c_uint32
    42 
    43 regfi.regfi_fetch_num_values.argtypes = [POINTER(REGFI_NK)]
    44 regfi.regfi_fetch_num_values.restype = c_uint32
    45 
    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_bool
    58 
    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_bool
    62 
    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_double
    76 
    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 = None
    82 
    83 regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)]
    84 regfi.regfi_iterator_down.restype = c_bool
    85 
    86 regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)]
    87 regfi.regfi_iterator_up.restype = c_bool
    88 
    89 regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)]
    90 regfi.regfi_iterator_to_root.restype = c_bool
    91 
    92 regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)]
    93 regfi.regfi_iterator_walk_path.restype = c_bool
    94 
    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_bool
    100 
    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_bool
    106 
    107 regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
    108 regfi.regfi_iterator_find_subkey.restype = c_bool
    109 
    110 regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)]
    111 regfi.regfi_iterator_first_value.restype = c_bool
    112 
    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_bool
    118 
    119 regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
    120 regfi.regfi_iterator_find_value.restype = c_bool
    121 
    122 
    123 regfi.regfi_init.argtypes = []
    124 regfi.regfi_init.restype = None
    125 regfi.regfi_init()
    12614
    12715
     
    15139        ret_val.append(s.encode('utf-8', 'replace'))
    15240
    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)
    15442    # Terminate the char** with a NULL pointer
    15543    ret_val[-1] = 0
     
    246134
    247135    def __getitem__(self, name):
    248         index = c_uint32()
     136        index = ctypes.c_uint32()
    249137        if isinstance(name, str):
    250138            name = name.encode('utf-8')
     
    253141            name = create_string_buffer(bytes(name))
    254142
    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,
    257146                                     self._get_element(self._hive.file,
    258147                                                       self._key._base,
     
    275164
    276165        elem = self._get_element(self._hive.file, self._key._base,
    277                                  c_uint32(self._current))
     166                                 ctypes.c_uint32(self._current))
    278167        self._current += 1
    279168        return self._constructor(self._hive, elem)
     
    493382# information.
    494383class HiveIterator():
    495     hive = None
    496     iter = None
    497     iteration_root = None
     384    _hive = None
     385    _iter = None
     386    _iteration_root = None
    498387
    499388    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:
    502391            raise Exception("Could not create iterator.  Current log:\n"
    503392                            + GetLogMessages())
     
    508397
    509398    def __del__(self):   
    510         regfi.regfi_iterator_free(self.iter)
     399        regfi.regfi_iterator_free(self._iter)
    511400
    512401    def __iter__(self):
    513         self.iteration_root = None
     402        self._iteration_root = None
    514403        return self
    515404
    516405    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)
    521410            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 = None
     411                   not regfi.regfi_iterator_next_subkey(self._iter)):
     412                if self._iteration_root == self.current_key():
     413                    self._iteration_root = None
    525414                    raise StopIteration('')
    526                 up_ret = regfi.regfi_iterator_up(self.iter)
     415                up_ret = regfi.regfi_iterator_up(self._iter)
    527416
    528417            if not up_ret:
     
    530419           
    531420            # XXX: Use non-generic exception
    532             if not regfi.regfi_iterator_down(self.iter):
     421            if not regfi.regfi_iterator_down(self._iter):
    533422                raise Exception('Error traversing iterator downward.'+
    534423                                ' Current log:\n'+ GetLogMessages())
    535424
    536         regfi.regfi_iterator_first_subkey(self.iter)
     425        regfi.regfi_iterator_first_subkey(self._iter)
    537426        return self.current_key()
    538427
     
    540429    next = __next__
    541430
    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))
    544439
    545440    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))
    547485
    548486    def descend(self, path):
     
    550488
    551489        # 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):
    553491            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  
    224224                           ('state', c_void_p),
    225225                           ]
     226
     227
     228# Load libregfi and define function prototypes
     229regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True)
     230
     231regfi.regfi_alloc.argtypes = [c_int, REGFI_ENCODING]
     232regfi.regfi_alloc.restype = POINTER(REGFI_FILE)
     233
     234regfi.regfi_alloc_cb.argtypes = [POINTER(REGFI_RAW_FILE), REGFI_ENCODING]
     235regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE)
     236
     237regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)]
     238regfi.regfi_free.restype = None
     239
     240regfi.regfi_log_get_str.argtypes = []
     241regfi.regfi_log_get_str.restype = c_char_p
     242
     243regfi.regfi_log_set_mask.argtypes = [c_uint16]
     244regfi.regfi_log_set_mask.restype = c_bool
     245
     246regfi.regfi_get_rootkey.argtypes = [POINTER(REGFI_FILE)]
     247regfi.regfi_get_rootkey.restype = POINTER(REGFI_NK)
     248
     249regfi.regfi_free_record.argtypes = [c_void_p]
     250regfi.regfi_free_record.restype = None
     251
     252regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)]
     253regfi.regfi_fetch_num_subkeys.restype = c_uint32
     254
     255regfi.regfi_fetch_num_values.argtypes = [POINTER(REGFI_NK)]
     256regfi.regfi_fetch_num_values.restype = c_uint32
     257
     258regfi.regfi_fetch_classname.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]
     259regfi.regfi_fetch_classname.restype = POINTER(REGFI_CLASSNAME)
     260
     261regfi.regfi_fetch_sk.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]
     262regfi.regfi_fetch_sk.restype = POINTER(REGFI_SK)
     263
     264regfi.regfi_fetch_data.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_VK)]
     265regfi.regfi_fetch_data.restype = POINTER(REGFI_DATA)
     266
     267regfi.regfi_find_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     268                                    c_char_p, POINTER(c_uint32)]
     269regfi.regfi_find_subkey.restype = c_bool
     270
     271regfi.regfi_find_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     272                                    c_char_p, POINTER(c_uint32)]
     273regfi.regfi_find_value.restype = c_bool
     274
     275regfi.regfi_get_subkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     276                                   c_uint32]
     277regfi.regfi_get_subkey.restype = POINTER(REGFI_NK)
     278
     279regfi.regfi_get_value.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK),
     280                                   c_uint32]
     281regfi.regfi_get_value.restype = POINTER(REGFI_VK)
     282
     283regfi.regfi_get_parentkey.argtypes = [POINTER(REGFI_FILE), POINTER(REGFI_NK)]
     284regfi.regfi_get_parentkey.restype = POINTER(REGFI_NK)
     285
     286regfi.regfi_nt2unix_time.argtypes = [POINTER(REGFI_NTTIME)]
     287regfi.regfi_nt2unix_time.restype = c_double
     288
     289regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING]
     290regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR)
     291
     292regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)]
     293regfi.regfi_iterator_free.restype = None
     294
     295regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)]
     296regfi.regfi_iterator_down.restype = c_bool
     297
     298regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)]
     299regfi.regfi_iterator_up.restype = c_bool
     300
     301regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)]
     302regfi.regfi_iterator_to_root.restype = c_bool
     303
     304regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)]
     305regfi.regfi_iterator_walk_path.restype = c_bool
     306
     307regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)]
     308regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK)
     309
     310regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
     311regfi.regfi_iterator_first_subkey.restype = c_bool
     312
     313regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
     314regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK)
     315
     316regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)]
     317regfi.regfi_iterator_next_subkey.restype = c_bool
     318
     319regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
     320regfi.regfi_iterator_find_subkey.restype = c_bool
     321
     322regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)]
     323regfi.regfi_iterator_first_value.restype = c_bool
     324
     325regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)]
     326regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK)
     327
     328regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)]
     329regfi.regfi_iterator_next_value.restype = c_bool
     330
     331regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p]
     332regfi.regfi_iterator_find_value.restype = c_bool
     333
     334
     335regfi.regfi_init.argtypes = []
     336regfi.regfi_init.restype = None
     337regfi.regfi_init()
Note: See TracChangeset for help on using the changeset viewer.