source: trunk/python/pyregfi/__init__.py @ 205

Last change on this file since 205 was 205, checked in by tim, 14 years ago

wrapper data structures and part of iterator interface

File size: 5.5 KB
Line 
1#!/usr/bin/env python
2
3import sys
4from pyregfi.structures import *
5
6import ctypes
7import ctypes.util
8from ctypes import c_char,c_char_p,c_int,c_uint16,c_bool,POINTER
9
10regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True)
11
12
13regfi.regfi_alloc.argtypes = [c_int]
14regfi.regfi_alloc.restype = POINTER(REGFI_FILE)
15
16regfi.regfi_alloc_cb.argtypes = [POINTER(REGFI_RAW_FILE)]
17regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE)
18
19regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)]
20regfi.regfi_free.restype = None
21
22regfi.regfi_log_get_str.argtypes = []
23regfi.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
86
87regfi.regfi_init.argtypes = []
88regfi.regfi_init.restype = None
89regfi.regfi_init()
90
91
92def GetLogMessages():
93    return regfi.regfi_log_get_str()
94
95
96class Hive():   
97    file = None
98    raw_file = None
99   
100    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
109       
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)
115
116    def __getattr__(self, name):
117        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           
124
125    def __iter__(self):
126        return HiveIterator(self)
127
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 TracBrowser for help on using the repository browser.