1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import traceback |
---|
6 | import ctypes |
---|
7 | import ctypes.util |
---|
8 | from ctypes import c_char,c_int,c_uint8,c_uint16,c_uint32,c_uint64,c_int64,POINTER,c_void_p,c_char_p |
---|
9 | |
---|
10 | |
---|
11 | class REGFI_RAW_FILE(ctypes.Structure): |
---|
12 | fh = None |
---|
13 | |
---|
14 | def cb_seek(self, raw_file, offset, whence): |
---|
15 | try: |
---|
16 | self.fh.seek(offset, whence) |
---|
17 | except Exception: |
---|
18 | traceback.print_exc() |
---|
19 | # XXX: os.EX_IOERR may not be available on Windoze |
---|
20 | ctypes.set_errno(os.EX_IOERR) |
---|
21 | return -1 |
---|
22 | |
---|
23 | return self.fh.tell() |
---|
24 | |
---|
25 | |
---|
26 | def cb_read(self, raw_file, buf, count): |
---|
27 | try: |
---|
28 | # XXX: anyway to do a readinto() here? |
---|
29 | tmp = self.fh.read(count) |
---|
30 | ctypes.memmove(buf,tmp,len(tmp)) |
---|
31 | |
---|
32 | except Exception: |
---|
33 | traceback.print_exc() |
---|
34 | # XXX: os.EX_IOERR may not be available on Windoze |
---|
35 | ctypes.set_errno(os.EX_IOERR) |
---|
36 | return -1 |
---|
37 | return len(tmp) |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | # XXX: how can we know for sure the size of off_t and size_t? |
---|
42 | seek_cb_type = ctypes.CFUNCTYPE(c_int64, POINTER(REGFI_RAW_FILE), c_uint64, c_int, use_errno=True) |
---|
43 | read_cb_type = ctypes.CFUNCTYPE(c_int64, POINTER(REGFI_RAW_FILE), POINTER(c_char), c_uint64, use_errno=True) |
---|
44 | |
---|
45 | |
---|
46 | REGFI_RAW_FILE._fields_ = [('seek', seek_cb_type), |
---|
47 | ('read', read_cb_type), |
---|
48 | ('cur_off', c_uint64), |
---|
49 | ('size', c_uint64), |
---|
50 | ('state', c_void_p), |
---|
51 | ] |
---|
52 | |
---|
53 | |
---|
54 | class REGFI_FILE(ctypes.Structure): |
---|
55 | _fields_ = [('magic', c_char * 4), |
---|
56 | ('sequence1', c_uint32), |
---|
57 | ('sequence2', c_uint32), |
---|
58 | ('mtime', c_uint64), |
---|
59 | ('major_version', c_uint32), |
---|
60 | ('minor_version', c_uint32), |
---|
61 | ('type', c_uint32), |
---|
62 | ('format', c_uint32), |
---|
63 | ('root_cell', c_uint32), |
---|
64 | ('last_block', c_uint32), |
---|
65 | ('cluster', c_uint32), |
---|
66 | ] |
---|
67 | |
---|
68 | |
---|
69 | |
---|