简体   繁体   中英

How to deserialize/serialize byte array to structure in python?

I have this code in C#:

Structure:

[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;
    }

Login Func:

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;
    }

Now the buffer converter class:

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));
            }
        }
    }

The code abocve create a byte array with a login data, from a structure. Is there a way to do serialize/deserialize a buffer array in python? - I have no idea how to do it in python, since I don't see a lot of articles dealing with byte array stuff.

There's a couple ways, sure.

There's the built-in struct module , which requires a little bit of manual work to figure out the format string for your structures.

You can also use a higher-level 3rd party library such as construct (which I can recommend).

With Construct, your structures might look something like

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

– a fairly straightforward translation of the C# originals, and given a buffer of data, you can then just do something like

login_data = Login.parse(buffer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM