qcow2_format.py: use strings to specify c-type of struct fields
We are going to move field-parsing to super-class, this will be simpler with simple string specifiers instead of variables. For some reason, python doesn't allow the definition of ctypes variable in the class alongside fields: it would not be available then for use by the 'for' operator. Don't worry: ctypes will be moved to metaclass soon. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com> Message-Id: <20200606081806.23897-8-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
621ca4988a
commit
5432a0db52
|
@ -34,36 +34,42 @@ class QcowHeaderExtension:
|
||||||
return QcowHeaderExtension(magic, len(data), data)
|
return QcowHeaderExtension(magic, len(data), data)
|
||||||
|
|
||||||
|
|
||||||
class QcowHeader:
|
# Mapping from c types to python struct format
|
||||||
|
ctypes = {
|
||||||
|
'u8': 'B',
|
||||||
|
'u16': 'H',
|
||||||
|
'u32': 'I',
|
||||||
|
'u64': 'Q'
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t = 'I'
|
|
||||||
uint64_t = 'Q'
|
class QcowHeader:
|
||||||
|
|
||||||
fields = (
|
fields = (
|
||||||
# Version 2 header fields
|
# Version 2 header fields
|
||||||
(uint32_t, '{:#x}', 'magic'),
|
('u32', '{:#x}', 'magic'),
|
||||||
(uint32_t, '{}', 'version'),
|
('u32', '{}', 'version'),
|
||||||
(uint64_t, '{:#x}', 'backing_file_offset'),
|
('u64', '{:#x}', 'backing_file_offset'),
|
||||||
(uint32_t, '{:#x}', 'backing_file_size'),
|
('u32', '{:#x}', 'backing_file_size'),
|
||||||
(uint32_t, '{}', 'cluster_bits'),
|
('u32', '{}', 'cluster_bits'),
|
||||||
(uint64_t, '{}', 'size'),
|
('u64', '{}', 'size'),
|
||||||
(uint32_t, '{}', 'crypt_method'),
|
('u32', '{}', 'crypt_method'),
|
||||||
(uint32_t, '{}', 'l1_size'),
|
('u32', '{}', 'l1_size'),
|
||||||
(uint64_t, '{:#x}', 'l1_table_offset'),
|
('u64', '{:#x}', 'l1_table_offset'),
|
||||||
(uint64_t, '{:#x}', 'refcount_table_offset'),
|
('u64', '{:#x}', 'refcount_table_offset'),
|
||||||
(uint32_t, '{}', 'refcount_table_clusters'),
|
('u32', '{}', 'refcount_table_clusters'),
|
||||||
(uint32_t, '{}', 'nb_snapshots'),
|
('u32', '{}', 'nb_snapshots'),
|
||||||
(uint64_t, '{:#x}', 'snapshot_offset'),
|
('u64', '{:#x}', 'snapshot_offset'),
|
||||||
|
|
||||||
# Version 3 header fields
|
# Version 3 header fields
|
||||||
(uint64_t, 'mask', 'incompatible_features'),
|
('u64', 'mask', 'incompatible_features'),
|
||||||
(uint64_t, 'mask', 'compatible_features'),
|
('u64', 'mask', 'compatible_features'),
|
||||||
(uint64_t, 'mask', 'autoclear_features'),
|
('u64', 'mask', 'autoclear_features'),
|
||||||
(uint32_t, '{}', 'refcount_order'),
|
('u32', '{}', 'refcount_order'),
|
||||||
(uint32_t, '{}', 'header_length'),
|
('u32', '{}', 'header_length'),
|
||||||
)
|
)
|
||||||
|
|
||||||
fmt = '>' + ''.join(field[0] for field in fields)
|
fmt = '>' + ''.join(ctypes[f[0]] for f in fields)
|
||||||
|
|
||||||
def __init__(self, fd):
|
def __init__(self, fd):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue