如何在python中解析二进制文件中的序列化C结构?

时间:2015-10-03 23:03:01

标签: python c struct deserialization

我有一些不同类型的C结构,它们都被压缩成二进制文件。

struct-id serialized-struct struct-id serialized-struct ...

如果反复使用相同的结构,那么使用struct包是有意义的,但我想在以前定义的结构之间切换。

STRUCT1_ID = '\xAA'
STRUCT2_ID = '\xBB'
STRUCT_IDS = frozenset([STRUCT1_ID, STRUCT2_ID])

struct1s = []
struct2s = []

def create_test_file(filepath):
    with open(filepath, 'wb') as f:
        # Write an example struct1 id followed by struct
        f.write(STRUCT1_ID)
        f.write(b'\x01\x02\x03\x04\x05\x06')
        # Write an example struct2 id followed by struct
        f.write(STRUCT2_ID)
        f.write(b'\x07\x08\x09\x0A')

def parse_test_file(filepath):
    with open(filepath, 'rb') as f:
        msg_type = f.read(1)
        while msg_type:
            print(byte)
            if byte in STRUCT_IDS:
                # Parse the next however many bytes needed by struct
                # logic breaks down here
                struct1s.append(turnIntoStruct(f.read(?)))
                msg_type = f.read(1)
            else:
              print('Corrupted file.  Unrecognized id')

在C中,结构将是:

typedef struct struct1_s {
  uint16_t a;
  uint16_t b;
  uint16_t c;
} struct1_t;

typedef struct struct2_s {
  uint16_t d;
  uint16_t e;
} struct2_t;

// Declare and initialize the structs
struct1_t s1 = {
  .a = 0x0201,
  .b = 0x0403,
  .c = 0x0605
};

struct2_t s2 = {
  .d = 0x0807,
  .e = 0x0A09
};

我现在不像我现在那样蟒蛇。我似乎无法将构造带到python 3.4.3?

1 个答案:

答案 0 :(得分:1)

将ID映射到结构模式,并使用适当的模式。

structmap = {
  b'\xaa': ('3H', struct1s),
  b'\xbb': ('2H', struct2s)
}

 ...

structmap[msg_type][1].append(struct.unpack(structmap[msg_type][0],
  f.read(struct.calcsize(structmap[msg_type][0]))))
相关问题