Changeset 208
- Timestamp:
- 09/07/10 23:50:04 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/pyregfi/__init__.py
r207 r208 121 121 122 122 123 def _buffer2bytearray(char_pointer, length): 124 if length == 0 or char_pointer == None: 125 return None 126 127 ret_val = bytearray(length) 128 for i in range(0,length): 129 ret_val[i] = char_pointer[i][0] 130 131 return ret_val 132 133 134 135 123 136 class _StructureWrapper(): 124 137 "Handles memory management and proxies attribute access to base structures" … … 144 157 145 158 159 class Key(_StructureWrapper): 160 pass 161 146 162 class Value(_StructureWrapper): 147 163 pass 148 164 149 150 165 class Data(_StructureWrapper): 151 166 pass 152 167 153 154 168 class Security(_StructureWrapper): 155 169 pass 156 170 157 171 158 class _ ValueList():172 class _GenericList(): 159 173 hive = None 160 174 key = None … … 162 176 current = None 163 177 178 # implementation-specific functions 179 fetch_num = None 180 find_element = None 181 get_element = None 182 constructor = None 183 164 184 def __init__(self, key): 165 185 self.hive = key.hive 166 186 # XXX: check for NULL here, throw an exception if so. 167 187 self.key = key 168 self.length = regfi.regfi_fetch_num_values(key.base)188 self.length = self.fetch_num(key.base) 169 189 170 190 def __len__(self): … … 173 193 def __getitem__(self, name): 174 194 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)) 195 if isinstance(name, str): 196 name = name.encode('utf-8') 197 198 if self.find_element(self.hive.file, self.key.base, 199 create_string_buffer(name), byref(index)): 200 return self.constructor(self.hive, self.get_element(self.hive.file, 201 self.key.base, 202 index)) 180 203 raise KeyError('') 204 181 205 182 206 def __iter__(self): … … 188 212 raise StopIteration('') 189 213 190 vk = regfi.regfi_get_value(self.hive.file, self.key.base,191 214 elem = self.get_element(self.hive.file, self.key.base, 215 c_uint32(self.current)) 192 216 self.current += 1 193 return vk.contents 194 217 return elem.contents 218 219 220 class _SubkeyList(_GenericList): 221 fetch_num = regfi.regfi_fetch_num_subkeys 222 find_element = regfi.regfi_find_subkey 223 get_element = regfi.regfi_get_subkey 224 225 226 class _ValueList(_GenericList): 227 fetch_num = regfi.regfi_fetch_num_values 228 find_element = regfi.regfi_find_value 229 get_element = regfi.regfi_get_value 230 195 231 196 232 class Key(_StructureWrapper): 197 233 values = None 234 subkeys = None 198 235 199 236 def __init__(self, hive, base): 200 237 super(Key, self).__init__(hive, base) 201 238 self.values = _ValueList(self) 239 self.subkeys = _SubkeyList(self) 240 241 def __getattr__(self, name): 242 ret_val = super(Key, self).__getattr__(name) 243 if ret_val == None: 244 return None 245 246 if name == "name": 247 ret_val = ret_val.decode('utf-8') 248 elif name == "name_raw": 249 length = super(Key, self).__getattr__('name_length') 250 ret_val = _buffer2bytearray(ret_val, length) 251 252 return ret_val 253 202 254 203 255 def fetch_security(self): 204 256 return Security(self.hive, 205 257 regfi.regfi_fetch_sk(self.hive.file, self.base)) 258 259 260 class Value(_StructureWrapper): 261 def __getattr__(self, name): 262 ret_val = super(Value, self).__getattr__(name) 263 if ret_val == None: 264 return None 265 266 if name == "name": 267 ret_val = ret_val.decode('utf-8') 268 elif name == "name_raw": 269 length = super(Value, self).__getattr__('name_length') 270 ret_val = _buffer2bytearray(ret_val, length) 271 272 return ret_val 273 274 275 # Avoids chicken/egg class definitions. 276 # Also makes for convenient code reuse in these lists' parent classes. 277 _SubkeyList.constructor = Key 278 _ValueList.constructor = Value 279 206 280 207 281
Note: See TracChangeset
for help on using the changeset viewer.