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

How to parse serialized C structs from binary file in python?

我有一些不同类型的 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 还不如 C。我似乎无法将构造带入 python 3.4.3?

将 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]))))