Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
circuitpython/shared-bindings/struct/__init__.c at main · MicroPythonNexus/circuitpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
MicroPythonNexus
/
circuitpython
Public
forked from
adafruit/circuitpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
circuitpython
/
shared-bindings
/
struct
/
__init__.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
166 lines (141 loc) · 6 KB
main
Breadcrumbs
circuitpython
/
shared-bindings
/
struct
/
__init__.c
Copy path
Top
File metadata and controls
Code
Blame
166 lines (141 loc) · 6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
// SPDX-FileCopyrightText: Copyright (c) 2014 Paul Sokolovsky
// SPDX-FileCopyrightText: Copyright (c) 2017 Michael McWethy
//
// SPDX-License-Identifier: MIT
#include
<assert.h>
#include
<string.h>
#include
"py/runtime.h"
#include
"py/builtin.h"
#include
"py/objtuple.h"
#include
"py/binary.h"
#include
"py/parsenum.h"
#include
"shared-bindings/struct/__init__.h"
#include
"shared-module/struct/__init__.h"
//| """Manipulation of c-style data
//|
//| |see_cpython_module| :mod:`cpython:struct`.
//|
//| Supported size/byte order prefixes: *@*, *<*, *>*, *!*.
//|
//| Supported format codes: *b*, *B*, *x*, *h*, *H*, *i*, *I*, *l*, *L*, *q*, *Q*,
//| *s*, *P*, *f*, *d* (the latter 2 depending on the floating-point support)."""
//|
//|
//| def calcsize(fmt: str) -> int:
//| """Return the number of bytes needed to store the given fmt."""
//| ...
//|
//|
static
mp_obj_t
struct_calcsize
(
mp_obj_t
fmt_in
) {
return
MP_OBJ_NEW_SMALL_INT
(
shared_modules_struct_calcsize
(
fmt_in
));
}
MP_DEFINE_CONST_FUN_OBJ_1
(
struct_calcsize_obj
,
struct_calcsize
);
//| def pack(fmt: str, *values: Any) -> bytes:
//| """Pack the values according to the format string fmt.
//| The return value is a bytes object encoding the values."""
//| ...
//|
//|
static
mp_obj_t
struct_pack
(
size_t
n_args
,
const
mp_obj_t
*
args
) {
mp_int_t
size
=
MP_OBJ_SMALL_INT_VALUE
(
struct_calcsize
(
args
[
0
]));
vstr_t
vstr
;
vstr_init_len
(
&
vstr
,
size
);
byte
*
p
=
(
byte
*
)
vstr
.
buf
;
memset
(
p
,
0
,
size
);
byte
*
end_p
=
&
p
[
size
];
shared_modules_struct_pack_into
(
args
[
0
],
p
,
end_p
,
n_args
-
1
,
&
args
[
1
]);
return
mp_obj_new_bytes_from_vstr
(
&
vstr
);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
struct_pack_obj
,
1
,
MP_OBJ_FUN_ARGS_MAX
,
struct_pack
);
//| def pack_into(fmt: str, buffer: WriteableBuffer, offset: int, *values: Any) -> None:
//| """Pack the values according to the format string fmt into a buffer
//| starting at offset. offset may be negative to count from the end of buffer."""
//| ...
//|
//|
static
mp_obj_t
struct_pack_into
(
size_t
n_args
,
const
mp_obj_t
*
args
) {
mp_buffer_info_t
bufinfo
;
mp_get_buffer_raise
(
args
[
1
],
&
bufinfo
,
MP_BUFFER_WRITE
);
mp_int_t
offset
=
mp_obj_get_int
(
args
[
2
]);
if
(
offset
<
0
) {
// negative offsets are relative to the end of the buffer
offset
=
(
mp_int_t
)
bufinfo
.
len
+
offset
;
if
(
offset
<
0
) {
mp_raise_RuntimeError
(
MP_ERROR_TEXT
(
"Buffer too small"
));
}
}
byte
*
p
=
(
byte
*
)
bufinfo
.
buf
;
byte
*
end_p
=
&
p
[
bufinfo
.
len
];
p
+=
offset
;
shared_modules_struct_pack_into
(
args
[
0
],
p
,
end_p
,
n_args
-
3
,
&
args
[
3
]);
return
mp_const_none
;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
struct_pack_into_obj
,
3
,
MP_OBJ_FUN_ARGS_MAX
,
struct_pack_into
);
//| def unpack(fmt: str, data: ReadableBuffer) -> Tuple[Any, ...]:
//| """Unpack from the data according to the format string fmt. The return value
//| is a tuple of the unpacked values. The buffer size must match the size
//| required by the format."""
//| ...
//|
//|
static
mp_obj_t
struct_unpack
(
size_t
n_args
,
const
mp_obj_t
*
args
) {
mp_buffer_info_t
bufinfo
;
mp_get_buffer_raise
(
args
[
1
],
&
bufinfo
,
MP_BUFFER_READ
);
byte
*
p
=
bufinfo
.
buf
;
byte
*
end_p
=
&
p
[
bufinfo
.
len
];
// true means check the size must be exactly right.
return
MP_OBJ_FROM_PTR
(
shared_modules_struct_unpack_from
(
args
[
0
],
p
,
end_p
, true));
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
struct_unpack_obj
,
2
,
3
,
struct_unpack
);
//| def unpack_from(fmt: str, data: ReadableBuffer, offset: int = 0) -> Tuple[Any, ...]:
//| """Unpack from the data starting at offset according to the format string fmt.
//| offset may be negative to count from the end of buffer. The return value is
//| a tuple of the unpacked values. The buffer size must be at least as big
//| as the size required by the form."""
//| ...
//|
//|
static
mp_obj_t
struct_unpack_from
(
size_t
n_args
,
const
mp_obj_t
*
pos_args
,
mp_map_t
*
kw_args
) {
enum
{
ARG_format
,
ARG_buffer
,
ARG_offset
};
static
const
mp_arg_t
allowed_args
[]
=
{
{
MP_QSTR_format
,
MP_ARG_REQUIRED
|
MP_ARG_OBJ
, {} },
{
MP_QSTR_buffer
,
MP_ARG_REQUIRED
|
MP_ARG_OBJ
, {} },
{
MP_QSTR_offset
,
MP_ARG_INT
, {.
u_int
=
0
} },
};
mp_arg_val_t
args
[
MP_ARRAY_SIZE
(
allowed_args
)];
mp_arg_parse_all
(
n_args
,
pos_args
,
kw_args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed_args
,
args
);
mp_buffer_info_t
bufinfo
;
mp_get_buffer_raise
(
args
[
ARG_buffer
].
u_obj
,
&
bufinfo
,
MP_BUFFER_READ
);
byte
*
p
=
bufinfo
.
buf
;
byte
*
end_p
=
&
p
[
bufinfo
.
len
];
mp_int_t
offset
=
args
[
ARG_offset
].
u_int
;
if
(
offset
<
0
) {
// negative offsets are relative to the end of the buffer
offset
=
bufinfo
.
len
+
offset
;
if
(
offset
<
0
) {
mp_raise_RuntimeError
(
MP_ERROR_TEXT
(
"Buffer too small"
));
}
}
p
+=
offset
;
// false means the size doesn't have to be exact. struct.unpack_from() only requires
// that be buffer be big enough.
return
MP_OBJ_FROM_PTR
(
shared_modules_struct_unpack_from
(
args
[
ARG_format
].
u_obj
,
p
,
end_p
, false));
}
MP_DEFINE_CONST_FUN_OBJ_KW
(
struct_unpack_from_obj
,
0
,
struct_unpack_from
);
static
const
mp_rom_map_elem_t
mp_module_struct_globals_table
[]
=
{
{
MP_ROM_QSTR
(
MP_QSTR___name__
),
MP_ROM_QSTR
(
MP_QSTR_struct
) },
{
MP_ROM_QSTR
(
MP_QSTR_calcsize
),
MP_ROM_PTR
(
&
struct_calcsize_obj
) },
{
MP_ROM_QSTR
(
MP_QSTR_pack
),
MP_ROM_PTR
(
&
struct_pack_obj
) },
{
MP_ROM_QSTR
(
MP_QSTR_pack_into
),
MP_ROM_PTR
(
&
struct_pack_into_obj
) },
{
MP_ROM_QSTR
(
MP_QSTR_unpack
),
MP_ROM_PTR
(
&
struct_unpack_obj
) },
{
MP_ROM_QSTR
(
MP_QSTR_unpack_from
),
MP_ROM_PTR
(
&
struct_unpack_from_obj
) },
};
static
MP_DEFINE_CONST_DICT
(
mp_module_struct_globals
,
mp_module_struct_globals_table
);
const
mp_obj_module_t
struct_module
=
{
.
base
=
{
&
mp_type_module
},
.
globals
=
(
mp_obj_dict_t
*
)
&
mp_module_struct_globals
,
};
MP_REGISTER_MODULE
(
MP_QSTR_struct
,
struct_module
);
You can’t perform that action at this time.