Changeset 220


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

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • test/pyregfi-smoketest.py

    r219 r220  
    88def usage():
    99    sys.stderr.write("USAGE: pyregfi-smoketest.py hive1 [hive2 ...]\n")
     10
     11
     12
     13
     14# Uses the HiveIterator to walk all keys
     15# Gathers various (meaningless) statistics to exercise simple attribute access
     16# and to hopefully smoke out any bugs that can be identified by changing stats
     17def iterTallyNames(hive):
     18    key_count = 0
     19    key_lens = 0
     20    key_rawlens = 0
     21    value_count = 0
     22    value_lens = 0
     23    value_rawlens = 0
     24
     25    for k in hive:
     26        key_count += 1
     27        if k.name != None:
     28            key_lens += len(k.name)
     29        if k.name_raw != None:
     30            key_rawlens += len(k.name_raw)
     31
     32        for v in k.values:
     33            value_count += 1
     34            if v.name != None:
     35                value_lens += len(v.name)
     36            if v.name_raw != None:
     37                value_rawlens += len(v.name_raw)
     38
     39    print("  Counts: keys=%d, values=%d" % (key_count, value_count))
     40    print("  Total name length: keys=%d, values=%d" % (key_lens, value_lens))
     41    print("  Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens))
    1042
    1143
     
    2355
    2456    return path
    25 
    26 
    27 # Uses the HiveIterator to walk all keys
    28 # Gathers various (meaningless) statistics to exercise simple attribute access
    29 # and to hopefully smoke out any bugs that can be identified by changing stats
    30 def iterTallyNames(hive):
    31     key_count = 0
    32     key_lens = 0
    33     key_rawlens = 0
    34     value_count = 0
    35     value_lens = 0
    36     value_rawlens = 0
    37 
    38     for k in hive:
    39         key_count += 1
    40         if k.name != None:
    41             key_lens += len(k.name)
    42         if k.name_raw != None:
    43             key_rawlens += len(k.name_raw)
    44 
    45         for v in k.values:
    46             value_count += 1
    47             if v.name != None:
    48                 value_lens += len(v.name)
    49             if v.name_raw != None:
    50                 value_rawlens += len(v.name_raw)
    51 
    52     print("  Counts: keys=%d, values=%d" % (key_count, value_count))
    53     print("  Total name length: keys=%d, values=%d" % (key_lens, value_lens))
    54     print("  Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens))
    55 
    5657
    5758# For each key in the hive, this traverses the parent links up to the root,
     
    154155    print("  Modified stat: %f" % modified_stat)
    155156
     157
     158
     159def iterIterWalk(hive):
     160    sk_stat = 0.0
     161    v_stat = 0.0
     162    iter = pyregfi.HiveIterator(hive)
     163    for k in iter:
     164        path = getCurrentPath(k)
     165        try:
     166            hive_iter = hive.subtree(path)
     167            sk = hive_iter.first_subkey()
     168            while sk != None:
     169                ssk = hive_iter.find_subkey(sk.name)
     170                sk_stat += len(ssk.name)
     171                sk = hive_iter.next_subkey()
     172
     173            v = hive_iter.first_value()
     174            while v != None:
     175                vv = hive_iter.find_value(v.name)
     176                v_stat += len(vv.name)
     177                v = hive_iter.next_value()
     178
     179        except Exception as e:
     180            print("WARNING: Could not decend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e))
     181    print("   Subkey stat: %f" % sk_stat)
     182    print("   Value stat: %f" % v_stat)
     183
     184
     185
    156186if len(sys.argv) < 2:
    157187    usage()
     
    159189
    160190
    161 #tests = [("iterTallyNames",iterTallyNames),("iterParentWalk",iterParentWalk),("iterTallyData",iterTallyData),("recurseKeyTally",recurseKeyTally),("iterFetchRelated",iterFetchRelated),]
    162 tests = [("iterFetchRelated",iterFetchRelated),]
     191tests = [("iterTallyNames",iterTallyNames),
     192         ("iterParentWalk",iterParentWalk),
     193         ("iterTallyData",iterTallyData),
     194         ("recurseKeyTally",recurseKeyTally),
     195         ("iterFetchRelated",iterFetchRelated),
     196         ("iterIterWalk",iterIterWalk),]
     197
     198tests = [("iterIterWalk",iterIterWalk),]
     199
    163200
    164201files = []
  • 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.