Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
7a2f1669
Commit
7a2f1669
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
modstruct: Fix alignment handling issues.
Also, factor out mp_binary_get_int() function.
parent
5aa740c3
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/binary.c
+25
-19
25 additions, 19 deletions
py/binary.c
py/binary.h
+1
-0
1 addition, 0 deletions
py/binary.h
tests/basics/struct1.py
+4
-0
4 additions, 0 deletions
tests/basics/struct1.py
with
30 additions
and
19 deletions
py/binary.c
+
25
−
19
View file @
7a2f1669
...
@@ -125,6 +125,28 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
...
@@ -125,6 +125,28 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
return
MP_OBJ_NEW_SMALL_INT
(
val
);
return
MP_OBJ_NEW_SMALL_INT
(
val
);
}
}
machine_int_t
mp_binary_get_int
(
uint
size
,
bool
is_signed
,
bool
big_endian
,
byte
*
p
)
{
int
delta
;
if
(
!
big_endian
)
{
delta
=
-
1
;
p
+=
size
-
1
;
}
else
{
delta
=
1
;
}
machine_int_t
val
=
0
;
if
(
is_signed
&&
*
p
&
0x80
)
{
val
=
-
1
;
}
for
(
uint
i
=
0
;
i
<
size
;
i
++
)
{
val
<<=
8
;
val
|=
*
p
;
p
+=
delta
;
}
return
val
;
}
#define is_signed(typecode) (typecode > 'Z')
#define is_signed(typecode) (typecode > 'Z')
mp_obj_t
mp_binary_get_val
(
char
struct_type
,
char
val_type
,
byte
**
ptr
)
{
mp_obj_t
mp_binary_get_val
(
char
struct_type
,
char
val_type
,
byte
**
ptr
)
{
byte
*
p
=
*
ptr
;
byte
*
p
=
*
ptr
;
...
@@ -140,26 +162,10 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
...
@@ -140,26 +162,10 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
struct_type
=
'>'
;
struct_type
=
'>'
;
#endif
#endif
}
}
*
ptr
=
p
+
size
;
int
delta
;
machine_int_t
val
=
mp_binary_get_int
(
size
,
is_signed
(
val_type
),
(
struct_type
==
'>'
),
p
);
if
(
struct_type
==
'<'
)
{
delta
=
-
1
;
p
+=
size
-
1
;
}
else
{
delta
=
1
;
}
machine_int_t
val
=
0
;
if
(
is_signed
(
val_type
)
&&
*
p
&
0x80
)
{
val
=
-
1
;
}
for
(
uint
i
=
0
;
i
<
size
;
i
++
)
{
val
<<=
8
;
val
|=
*
p
;
p
+=
delta
;
}
*
ptr
+=
size
;
if
(
val_type
==
'O'
)
{
if
(
val_type
==
'O'
)
{
return
(
mp_obj_t
)
val
;
return
(
mp_obj_t
)
val
;
}
else
if
(
val_type
==
'S'
)
{
}
else
if
(
val_type
==
'S'
)
{
...
@@ -185,6 +191,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
...
@@ -185,6 +191,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
struct_type
=
'>'
;
struct_type
=
'>'
;
#endif
#endif
}
}
*
ptr
=
p
+
size
;
#if MP_ENDIANNESS_BIG
#if MP_ENDIANNESS_BIG
#error Not implemented
#error Not implemented
...
@@ -215,7 +222,6 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
...
@@ -215,7 +222,6 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
in
+=
in_delta
;
in
+=
in_delta
;
}
}
*
ptr
+=
size
;
}
}
void
mp_binary_set_val_array
(
char
typecode
,
void
*
p
,
int
index
,
mp_obj_t
val_in
)
{
void
mp_binary_set_val_array
(
char
typecode
,
void
*
p
,
int
index
,
mp_obj_t
val_in
)
{
...
...
This diff is collapsed.
Click to expand it.
py/binary.h
+
1
−
0
View file @
7a2f1669
...
@@ -34,3 +34,4 @@ void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in)
...
@@ -34,3 +34,4 @@ void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in)
void
mp_binary_set_val_array_from_int
(
char
typecode
,
void
*
p
,
int
index
,
machine_int_t
val
);
void
mp_binary_set_val_array_from_int
(
char
typecode
,
void
*
p
,
int
index
,
machine_int_t
val
);
mp_obj_t
mp_binary_get_val
(
char
struct_type
,
char
val_type
,
byte
**
ptr
);
mp_obj_t
mp_binary_get_val
(
char
struct_type
,
char
val_type
,
byte
**
ptr
);
void
mp_binary_set_val
(
char
struct_type
,
char
val_type
,
mp_obj_t
val_in
,
byte
**
ptr
);
void
mp_binary_set_val
(
char
struct_type
,
char
val_type
,
mp_obj_t
val_in
,
byte
**
ptr
);
machine_int_t
mp_binary_get_int
(
uint
size
,
bool
is_signed
,
bool
big_endian
,
byte
*
p
);
This diff is collapsed.
Click to expand it.
tests/basics/struct1.py
+
4
−
0
View file @
7a2f1669
...
@@ -21,3 +21,7 @@ print(struct.calcsize("100sI"))
...
@@ -21,3 +21,7 @@ print(struct.calcsize("100sI"))
print
(
struct
.
calcsize
(
"
97sI
"
))
print
(
struct
.
calcsize
(
"
97sI
"
))
print
(
struct
.
unpack
(
"
<6sH
"
,
b
"
foo
\0\0\0\x12\x34
"
))
print
(
struct
.
unpack
(
"
<6sH
"
,
b
"
foo
\0\0\0\x12\x34
"
))
print
(
struct
.
pack
(
"
<6sH
"
,
b
"
foo
"
,
10000
))
print
(
struct
.
pack
(
"
<6sH
"
,
b
"
foo
"
,
10000
))
s
=
struct
.
pack
(
"
BHBI
"
,
10
,
100
,
200
,
300
)
v
=
struct
.
unpack
(
"
BHBI
"
,
s
)
print
(
v
==
(
10
,
100
,
200
,
300
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment