Python 3.1.3 ctypes.struct 未正确排序位并意外修改数据

发布于 2024-10-16 02:00:05 字数 787 浏览 8 评论 0原文

我定义了以下结构,

import ctypes
from ctypes import *
class r( BigEndianStructure ):
    _fields_ = [( "d0", c_uint32, 28 ),
                ( "d1", c_uint32, 18 ),
                ( "d2", c_uint32, 18 ),
                ( "d3", c_uint16, 16 ),
                ( "d4", c_uint16, 16 ), ]

然后使用以下代码进行测试

a = r(0xAAAAAAAA,0xBBBBBBBB,0xCCCCCCCC,0xDDDD,0xEEEE)
for byte in string_at( addressof( a ), sizeof( a ) ):
    print(hex(byte),end="")

,结果是

0xaa 0xaa 0xaa 0xa0 0xee 0xee 0xc0 0x0 0x33 0x33 0x0 0x0 0xdd 0xdd 0xee 0xee

预期结果

0xaa 0xaa 0xaa 0xa0 0xbb 0xbb 0xc0 0x0 0xcc 0xcc 0xc0 0x0 0xdd 0xdd 0xee 0xee

不仅结构未压缩,而且结果数据与输入的数据不同。是我犯了什么错误还是Python喜欢用自己的想法修改数据?

I defined the following structure

import ctypes
from ctypes import *
class r( BigEndianStructure ):
    _fields_ = [( "d0", c_uint32, 28 ),
                ( "d1", c_uint32, 18 ),
                ( "d2", c_uint32, 18 ),
                ( "d3", c_uint16, 16 ),
                ( "d4", c_uint16, 16 ), ]

then tested with the following code

a = r(0xAAAAAAAA,0xBBBBBBBB,0xCCCCCCCC,0xDDDD,0xEEEE)
for byte in string_at( addressof( a ), sizeof( a ) ):
    print(hex(byte),end="")

result is

0xaa 0xaa 0xaa 0xa0 0xee 0xee 0xc0 0x0 0x33 0x33 0x0 0x0 0xdd 0xdd 0xee 0xee

the expected result was

0xaa 0xaa 0xaa 0xa0 0xbb 0xbb 0xc0 0x0 0xcc 0xcc 0xc0 0x0 0xdd 0xdd 0xee 0xee

not only the structure was not compacted, the result data is different than what was entered. did I made any mistake or Python likes to modify data with its own mind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-10-23 02:00:05

看起来问题来自于将 18 位宽度值存储在 32 位中,然后将它们解释为完整的 32 位值。

让我们看看 0xBBBBBBBB 发生了什么:

0xBBBBBBBB = 10111011101110111011101110111011b
0xBBBBBBBB & 3FFFF (bit width of 18) = 111011101110111011b

如果您的结构看起来像 | d0 | 4 个填充位 | d1 | 14 个填充位 | d2 | 14 个填充位 ... | 然后将 d1 的地址解释为 32 位值将是:

11101110111011101100000000000000b = 0xEEEEC000

基本上在像这样读取内存时,而不是获取该值的 18 位掩码如您所料,您将得到 14 位移位。

It looks like the issue comes from storing the 18 bit width values in 32 bits, and then interpreting them as full 32 bit values.

Lets look at what is happening with 0xBBBBBBBB:

0xBBBBBBBB = 10111011101110111011101110111011b
0xBBBBBBBB & 3FFFF (bit width of 18) = 111011101110111011b

If your structures looks like | d0 | 4 pad bits | d1 | 14 pad bits | d2 | 14 pad bits ... | then interpreting the address of d1 as a 32 bit value will be:

11101110111011101100000000000000b = 0xEEEEC000

Basically when reading the memory like this, instead of getting the 18 bit mask of the value you expect, you are getting a 14 bit shift.

花桑 2024-10-23 02:00:05

字段左对齐(向右填充)

字段 d0 -

(AAAA AAAA & FFFFFFF (28 bits)) << (32 - 28 = 4) = AAAA AAA0

字段 d1 -

(BBBB BBBB & 3FFFF (18 bits)) << (32 - 18 = 14)  = EEEE C000

字段 d2 -

(CCCC CCCC & 3FFFF (18 bits)) << (32 - 18 = 14) = 3333 0000

字段 d1 和字段d2 不能同时容纳在 32 位字段中 - 因此 d2 在下一个 32 位槽中对齐。

字段 d1 的说明性分步示例:

BBBB BBBB & 3FFFF (only least-significant 18 bits kept) = 3BBBB

3BBBB << 14 (pad the last 14 bits) = EEEE C000

The fields are left aligned (padded to the right)

Field d0 -

(AAAA AAAA & FFFFFFF (28 bits)) << (32 - 28 = 4) = AAAA AAA0

Field d1 -

(BBBB BBBB & 3FFFF (18 bits)) << (32 - 18 = 14)  = EEEE C000

Field d2 -

(CCCC CCCC & 3FFFF (18 bits)) << (32 - 18 = 14) = 3333 0000

Fields d1 & d2 will not both fit within a 32 bit field - so d2 is aligned in the next 32 bit slot.

Illustrative step-by-step example for field d1:

BBBB BBBB & 3FFFF (only least-significant 18 bits kept) = 3BBBB

3BBBB << 14 (pad the last 14 bits) = EEEE C000
紅太極 2024-10-23 02:00:05

使用适合容器类型的位字段以避免对齐填充。在下面的示例中,4+8+16 适合 c_uint32,但 4+8+16+5 不适合,因此 d3 在下一个 c_uint32 中对齐:

from ctypes import *
class r( BigEndianStructure ):
    _fields_ = [('d0',c_uint32, 4),
                ('d1',c_uint32, 8),
                ('d2',c_uint32,16),
                ('d3',c_uint32, 5)]

def fld(n):
    return '[' + '-'*(n-2) + ']'

def pad(n):
    return '.'*n

print(fld(4),fld(8),fld(16),pad(4),fld(5),pad(27),sep='')

for i in range(1,17):
    v = 2**i-1
    a = r(v,v,v,v)
    for byte in string_at( addressof( a ), sizeof( a ) ):
        print('{0:08b}'.format(byte),end='',sep='')
    print()

输出

二进制输出使数字更易于可视化。请注意,第一个 c_uint32 的剩余 4 位无法容纳 5 位字段,因此添加了 4 位填充以开始下一个 c_uint32 中的 5 位字段。

[--][------][--------------]....[---]...........................
0001000000010000000000000001000000001000000000000000000000000000
0011000000110000000000000011000000011000000000000000000000000000
0111000001110000000000000111000000111000000000000000000000000000
1111000011110000000000001111000001111000000000000000000000000000
1111000111110000000000011111000011111000000000000000000000000000
1111001111110000000000111111000011111000000000000000000000000000
1111011111110000000001111111000011111000000000000000000000000000
1111111111110000000011111111000011111000000000000000000000000000
1111111111110000000111111111000011111000000000000000000000000000
1111111111110000001111111111000011111000000000000000000000000000
1111111111110000011111111111000011111000000000000000000000000000
1111111111110000111111111111000011111000000000000000000000000000
1111111111110001111111111111000011111000000000000000000000000000
1111111111110011111111111111000011111000000000000000000000000000
1111111111110111111111111111000011111000000000000000000000000000
1111111111111111111111111111000011111000000000000000000000000000

Use bit fields that fit in a container type to avoid alignment padding. In the example below, 4+8+16 fit in c_uint32, but 4+8+16+5 does not, so d3 aligns in the next c_uint32:

from ctypes import *
class r( BigEndianStructure ):
    _fields_ = [('d0',c_uint32, 4),
                ('d1',c_uint32, 8),
                ('d2',c_uint32,16),
                ('d3',c_uint32, 5)]

def fld(n):
    return '[' + '-'*(n-2) + ']'

def pad(n):
    return '.'*n

print(fld(4),fld(8),fld(16),pad(4),fld(5),pad(27),sep='')

for i in range(1,17):
    v = 2**i-1
    a = r(v,v,v,v)
    for byte in string_at( addressof( a ), sizeof( a ) ):
        print('{0:08b}'.format(byte),end='',sep='')
    print()

Output

Binary output makes the numbers easier to visualize. Note that the 5-bit field couldn't fit in the remaining 4 bits of the first c_uint32, so 4 bits of padding were added to start the 5-bit field in the next c_uint32.

[--][------][--------------]....[---]...........................
0001000000010000000000000001000000001000000000000000000000000000
0011000000110000000000000011000000011000000000000000000000000000
0111000001110000000000000111000000111000000000000000000000000000
1111000011110000000000001111000001111000000000000000000000000000
1111000111110000000000011111000011111000000000000000000000000000
1111001111110000000000111111000011111000000000000000000000000000
1111011111110000000001111111000011111000000000000000000000000000
1111111111110000000011111111000011111000000000000000000000000000
1111111111110000000111111111000011111000000000000000000000000000
1111111111110000001111111111000011111000000000000000000000000000
1111111111110000011111111111000011111000000000000000000000000000
1111111111110000111111111111000011111000000000000000000000000000
1111111111110001111111111111000011111000000000000000000000000000
1111111111110011111111111111000011111000000000000000000000000000
1111111111110111111111111111000011111000000000000000000000000000
1111111111111111111111111111000011111000000000000000000000000000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文