如何在python中反序列化/序列化字节数组以结构化?

时间:2019-06-17 13:32:19

标签: c# python python-3.x

我在C#中有此代码:

结构:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi, Size = 116)]
    public struct pLogin
    {
        public pHeader _header;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]
        public string senha;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]
        public string login;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] unk1;
        public int algo1;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 42)]
        public byte[] unk2;
        public short algo2;
        //versao do cliente
        public ushort cliver;
        public ushort unk3;
        public int umBool;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] mac;
    }
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi, Size = 12)]
    public struct pHeader
    {
        public ushort size;
        public byte key;
        public byte checksum;
        public ushort packetId;
        public ushort clientId;
        public uint timestamp;
    }

登录功能:

pLogin pLogin;
    public void iniciarLogin(string login, string senha, int cliver, string p_mac = "")
    {
        pLogin = new pLogin();
        pLogin._header = buildHeader(0x20D, 116);
        pLogin.senha = senha;
        pLogin.login = login;
        pLogin.cliver = (ushort)cliver;
        pLogin.umBool = 1;
        pLogin.algo1 = 132;
        pLogin.algo2 = 152;
        if (p_mac.Length == 0)
        {
            pLogin.mac = Encoding.ASCII.GetBytes(Functions.RandomString(16));
        }
        else
        {
            pLogin.mac = Functions.StringToByteArray(p_mac);
        }
        byte[] buffer = BufferConverter.StructureToBuffer<pLogin>(pLogin);
        EncDec.Encrypt(ref buffer);
        socket.Send(BufferConverter.StringToByteArray("11F3111F"));

        socket.Send(buffer);
        logger.Text += "[Cliente] Solicitando login...\n";

    }
pHeader packetHeader;
    private pHeader buildHeader(int _packetID, int size)
    {
        packetHeader = new pHeader();
        packetHeader.size = (ushort)size;
        packetHeader.key = EncDec.GetHashByte();
        packetHeader.checksum = 0;
        packetHeader.packetId = (ushort)_packetID;
        packetHeader.clientId = (ushort)serverData.playerMob.Mob.ClientId;
        packetHeader.timestamp = getCurrentTime();
        return packetHeader;
    }

现在缓冲转换器类:

public static Byte[] StructureToBuffer<T>(T structure)
    {
        Byte[] buffer = new Byte[Marshal.SizeOf(typeof(T))];

        unsafe
        {
            fixed (byte* pBuffer = buffer)
            {
                Marshal.StructureToPtr(structure, new IntPtr((void*)pBuffer), true);
            }
        }

        return buffer;
    }

    public static T BufferToStructure<T>(Byte[] buffer, Int32 offset)
    {
        unsafe
        {
            fixed (Byte* pBuffer = buffer)
            {
                return (T)Marshal.PtrToStructure(new IntPtr((void*)&pBuffer[offset]), typeof(T));
            }
        }
    }

以上代码从一个结构中创建一个带有登录数据的字节数组。 有没有办法在python中对缓冲区数组进行序列化/反序列化? - 我不知道如何在python中执行此操作,因为我看不到很多有关字节数组内容的文章。

1 个答案:

答案 0 :(得分:1)

肯定有几种方法。

built-in struct module,需要一些手工工作才能确定结构的格式字符串。

您还可以使用更高级别的第三方库,例如construct(我推荐)。

使用Construct,您的结构可能看起来像

Header = Struct(
    'size' / Int16ub,
    'key' / Int8ub,
    'checksum' / Int8ub,
    'packetId' / Int16ub,
    'clientId' / Int16ub,
    'timestamp' / Int32ub,
)
Login = Struct(
    "header" / Header,
    # ...
)

– C#原始文件的相当直接的翻译,并提供了数据缓冲区,然后您可以执行

之类的操作
login_data = Login.parse(buffer)