BIG_ENDIAN = '>'
module-attribute
Big-endian, std. size & alignment
LITTLE_ENDIAN = '<'
module-attribute
Little-endian, std. size & alignment
NATIVE_ORDER = '@'
module-attribute
Native order, size & alignment
CEnum
Bases: AbstractCEnum
Source code in cstruct/cenum.py
4 5 6 7 8 |
|
sizeof()
classmethod
Type size (in bytes)
Source code in cstruct/cenum.py
5 6 7 8 |
|
CStruct
Bases: AbstractCStruct
Convert C struct definitions into Python classes.
Attributes:
Name | Type | Description |
---|---|---|
__struct__ |
str
|
definition of the struct (or union) in C syntax |
__byte_order__ |
str
|
byte order, valid values are LITTLE_ENDIAN, BIG_ENDIAN, NATIVE_ORDER |
__is_union__ |
bool
|
True for union definitions, False for struct definitions |
__size__ |
int
|
size of the structure in bytes (flexible array member size is omitted) |
__fields__ |
list
|
list of structure fields |
__fields_types__ |
dict
|
dictionary mapping field names to types |
Source code in cstruct/cstruct.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
pack()
Pack the structure data into bytes
Returns:
Name | Type | Description |
---|---|---|
bytes |
bytes
|
The packed structure |
Source code in cstruct/cstruct.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
unpack_from(buffer, offset=0, flexible_array_length=None)
Unpack bytes containing packed C structure data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
buffer |
Optional[bytes]
|
bytes to be unpacked |
required |
offset |
int
|
optional buffer offset |
0
|
flexible_array_length |
Optional[int]
|
optional flexible array lenght (number of elements) |
None
|
Source code in cstruct/cstruct.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
MemCStruct
Bases: AbstractCStruct
Convert C struct definitions into Python classes.
Attributes:
Name | Type | Description |
---|---|---|
__struct__ |
str
|
definition of the struct (or union) in C syntax |
__byte_order__ |
str
|
byte order, valid values are LITTLE_ENDIAN, BIG_ENDIAN, NATIVE_ORDER |
__is_union__ |
bool
|
True for union definitions, False for struct definitions |
__mem__ |
mutable character buffer |
|
__size__ |
int
|
size of the structure in bytes (flexible array member size is omitted) |
__fields__ |
list
|
list of structure fields |
__fields_types__ |
dict
|
dictionary mapping field names to types |
Source code in cstruct/mem_cstruct.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
memcpy(destination, source, num)
Copies the values of num bytes from source to the struct memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
destination |
int
|
destination address |
required |
source |
bytes
|
source data to be copied |
required |
num |
int
|
number of bytes to copy |
required |
Source code in cstruct/mem_cstruct.py
87 88 89 90 91 92 93 94 95 96 |
|
pack()
Pack the structure data into bytes
Returns:
Name | Type | Description |
---|---|---|
bytes |
bytes
|
The packed structure |
Source code in cstruct/mem_cstruct.py
98 99 100 101 102 103 104 105 |
|
set_flexible_array_length(flexible_array_length)
Set flexible array length (i.e. number of elements)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
flexible_array_length |
Optional[int]
|
flexible array length |
required |
Source code in cstruct/mem_cstruct.py
107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
unpack_from(buffer, offset=0, flexible_array_length=None)
Unpack bytes containing packed C structure data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
buffer |
Optional[bytes]
|
bytes to be unpacked |
required |
offset |
int
|
optional buffer offset |
0
|
flexible_array_length |
Optional[int]
|
optional flexible array lenght (number of elements) |
None
|
Source code in cstruct/mem_cstruct.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
define(key, value)
Define a constant that can be used in the C struct
Examples:
>>> define("INIT_THREAD_SIZE", 16384)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
identifier |
required |
value |
Any
|
value of the constant |
required |
Source code in cstruct/__init__.py
66 67 68 69 70 71 72 73 74 75 76 77 |
|
get_type(type_)
Get a data type (struct, union, enum) by name
Examples:
>>> get_type("struct Position")
<class 'abc.Position'>
>>> get_type("enum htmlfont")
<enum 'htmlfont'>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ |
str
|
C type, struct or union (e.g. 'short int' or 'struct ZYZ'), enum or native type |
required |
Returns:
Name | Type | Description |
---|---|---|
class |
Any
|
data type class |
Raises:
Type | Description |
---|---|
KeyError
|
If type is not defined |
Source code in cstruct/__init__.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
getdef(key)
Return the value for a constant
Examples:
>>> define("INIT_THREAD_SIZE", 16384)
>>> getdef("INIT_THREAD_SIZE")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
identifier |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If key is not defined |
Source code in cstruct/__init__.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
parse(__struct__, __cls__=None, **kargs)
Return a new class mapping a C struct/union/enum definition. If the string does not contains any definition, return None. If the string contains multiple struct/union/enum definitions, returns the last definition.
Examples:
>>> cstruct.parse('struct Pair { unsigned char a; unsigned char b; };')
<class 'abc.Pair'>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__struct__ |
str
|
definition of the struct (or union/enum) in C syntax |
required |
__cls__ |
type
|
super class - CStruct(default) or MemCStruct |
None
|
__byte_order__ |
str
|
byte order, valid values are LITTLE_ENDIAN, BIG_ENDIAN, NATIVE_ORDER |
required |
Returns:
Name | Type | Description |
---|---|---|
cls |
Union[Type[AbstractCStruct], Type[AbstractCEnum], None]
|
cls subclass |
Raises:
Type | Description |
---|---|
cstruct.exceptions.ParserError
|
Parsing exception |
Source code in cstruct/__init__.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
sizeof(type_)
Return the size of the type.
Examples:
>>> sizeof("struct Position")
16
>>> sizeof('enum htmlfont')
4
>>> sizeof("int")
4
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ |
str
|
C type, struct or union (e.g. 'short int' or 'struct ZYZ'), enum or native type |
required |
Returns:
Name | Type | Description |
---|---|---|
size |
int
|
size in bytes |
Raises:
Type | Description |
---|---|
KeyError
|
If type is not defined |
Source code in cstruct/__init__.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
typedef(type_, alias)
Define an alias name for a data type
Examples:
>>> typedef("int", "status")
>>> sizeof("status")
4
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ |
str
|
data type |
required |
alias |
str
|
new alias name |
required |
Source code in cstruct/__init__.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
undef(key)
Undefine a symbol that was previously defined with define
Examples:
>>> define("INIT_THREAD_SIZE", 16384)
>>> undef("INIT_THREAD_SIZE")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
identifier |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If key is not defined |
Source code in cstruct/__init__.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|