Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
9336ee32
Commit
9336ee32
authored
Oct 06, 2014
by
Damien George
Browse files
py: Make mp_binary_set_val work on big endian machine.
parent
fcdb2398
Changes
4
Hide whitespace changes
Inline
Side-by-side
extmod/moductypes.c
View file @
9336ee32
...
...
@@ -401,7 +401,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
set_aligned_basic
(
val_type
&
6
,
self
->
addr
+
offset
,
val
);
}
else
{
mp_binary_set_int
(
GET_SCALAR_SIZE
(
val_type
&
7
),
self
->
flags
==
LAYOUT_BIG_ENDIAN
,
self
->
addr
+
offset
,
(
byte
*
)
&
val
);
self
->
addr
+
offset
,
val
);
}
return
set_val
;
// just !MP_OBJ_NULL
}
...
...
py/binary.c
View file @
9336ee32
...
...
@@ -140,23 +140,23 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
// The long long type is guaranteed to hold at least 64 bits, and size is at
// most 8 (for q and Q), so we will always be able to parse the given data
// and fit it into a long long.
long
long
mp_binary_get_int
(
mp_uint_t
size
,
bool
is_signed
,
bool
big_endian
,
byte
*
p
)
{
long
long
mp_binary_get_int
(
mp_uint_t
size
,
bool
is_signed
,
bool
big_endian
,
const
byte
*
src
)
{
int
delta
;
if
(
!
big_endian
)
{
delta
=
-
1
;
p
+=
size
-
1
;
src
+=
size
-
1
;
}
else
{
delta
=
1
;
}
long
long
val
=
0
;
if
(
is_signed
&&
*
p
&
0x80
)
{
if
(
is_signed
&&
*
src
&
0x80
)
{
val
=
-
1
;
}
for
(
uint
i
=
0
;
i
<
size
;
i
++
)
{
val
<<=
8
;
val
|=
*
p
;
p
+=
delta
;
val
|=
*
src
;
src
+=
delta
;
}
return
val
;
...
...
@@ -201,20 +201,22 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
}
void
mp_binary_set_int
(
mp_uint_t
val_sz
,
bool
big_endian
,
byte
*
p
,
byte
*
val_ptr
)
{
i
nt
in_delta
,
out_delta
;
if
(
big_endian
)
{
in_delta
=
-
1
;
out_delta
=
1
;
val_ptr
+=
val_sz
-
1
;
void
mp_binary_set_int
(
mp_uint_t
val_sz
,
bool
big_endian
,
byte
*
dest
,
mp_uint_t
val
)
{
i
f
(
MP_ENDIANNESS_LITTLE
&&
!
big_endian
)
{
memcpy
(
dest
,
&
val
,
val_sz
);
}
else
if
(
MP_ENDIANNESS_BIG
&&
big_endian
)
{
// only copy the least-significant val_sz bytes
memcpy
(
dest
,
(
byte
*
)
&
val
+
sizeof
(
mp_uint_t
)
-
val_sz
,
val_sz
)
;
}
else
{
in_delta
=
out_delta
=
1
;
}
for
(
uint
i
=
val_sz
;
i
>
0
;
i
--
)
{
*
p
=
*
val_ptr
;
p
+=
out_delta
;
val_ptr
+=
in_delta
;
const
byte
*
src
;
if
(
MP_ENDIANNESS_LITTLE
)
{
src
=
(
const
byte
*
)
&
val
+
val_sz
;
}
else
{
src
=
(
const
byte
*
)
&
val
+
sizeof
(
mp_uint_t
);
}
while
(
val_sz
--
)
{
*
dest
++
=
*--
src
;
}
}
}
...
...
@@ -226,28 +228,24 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
if
(
struct_type
==
'@'
)
{
// Make pointer aligned
p
=
(
byte
*
)(((
mp_uint_t
)
p
+
align
-
1
)
&
~
((
mp_uint_t
)
align
-
1
));
#
if MP_ENDIANNESS_LITTLE
struct_type
=
'<'
;
#
else
struct_type
=
'>'
;
#endif
if
(
MP_ENDIANNESS_LITTLE
)
{
struct_type
=
'<'
;
}
else
{
struct_type
=
'>'
;
}
}
*
ptr
=
p
+
size
;
#if MP_ENDIANNESS_BIG
#error Not implemented
#endif
mp_int_t
val
;
byte
*
in
=
(
byte
*
)
&
val
;
mp_uint_t
val
;
switch
(
val_type
)
{
case
'O'
:
in
=
(
byte
*
)
&
val_in
;
val
=
(
mp_uint_t
)
val_in
;
break
;
default:
val
=
mp_obj_get_int
(
val_in
);
}
mp_binary_set_int
(
MIN
(
size
,
sizeof
(
val
)),
struct_type
==
'>'
,
p
,
in
);
mp_binary_set_int
(
MIN
(
size
,
sizeof
(
val
)),
struct_type
==
'>'
,
p
,
val
);
}
void
mp_binary_set_val_array
(
char
typecode
,
void
*
p
,
mp_uint_t
index
,
mp_obj_t
val_in
)
{
...
...
py/binary.h
View file @
9336ee32
...
...
@@ -34,5 +34,5 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v
void
mp_binary_set_val_array_from_int
(
char
typecode
,
void
*
p
,
mp_uint_t
index
,
mp_int_t
val
);
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
);
long
long
mp_binary_get_int
(
mp_uint_t
size
,
bool
is_signed
,
bool
big_endian
,
byte
*
p
);
void
mp_binary_set_int
(
mp_uint_t
val_sz
,
bool
big_endian
,
byte
*
p
,
byte
*
val_ptr
);
long
long
mp_binary_get_int
(
mp_uint_t
size
,
bool
is_signed
,
bool
big_endian
,
const
byte
*
src
);
void
mp_binary_set_int
(
mp_uint_t
val_sz
,
bool
big_endian
,
byte
*
dest
,
mp_uint_t
val
);
py/modstruct.c
View file @
9336ee32
...
...
@@ -160,7 +160,7 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
}
MP_DEFINE_CONST_FUN_OBJ_2
(
struct_unpack_obj
,
struct_unpack
);
STATIC
mp_obj_t
struct_pack
(
uint
n_args
,
mp_obj_t
*
args
)
{
STATIC
mp_obj_t
struct_pack
(
mp_
uint
_t
n_args
,
const
mp_obj_t
*
args
)
{
// TODO: "The arguments must match the values required by the format exactly."
const
char
*
fmt
=
mp_obj_str_get_str
(
args
[
0
]);
char
fmt_type
=
get_fmt_type
(
&
fmt
);
...
...
@@ -169,7 +169,7 @@ STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) {
mp_obj_t
res
=
mp_obj_str_builder_start
(
&
mp_type_bytes
,
size
,
&
p
);
memset
(
p
,
0
,
size
);
for
(
uint
i
=
1
;
i
<
n_args
;
i
++
)
{
for
(
mp_
uint
_t
i
=
1
;
i
<
n_args
;
i
++
)
{
mp_uint_t
sz
=
1
;
if
(
unichar_isdigit
(
*
fmt
))
{
sz
=
get_fmt_num
(
&
fmt
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment