BattleNet.Huffman.Decompress C# (CSharp) Метод

Decompress() публичный статический Метод

public static Decompress ( byte input, byte &output ) : void
input byte
output byte
Результат void
        public static void Decompress(byte[] input, out byte[] output)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            var size = input.Length;

            var result = new byte[1024];

            uint b = 0;

            uint i = 0;
            var max = (uint)result.Length;
            var j = 0;
            uint count = 0x20;

            while (true)
            {
                uint a;
                if (count >= 8)
                {
                    while (size > 0 && count >= 8)
                    {
                        count -= 8;
                        size--;
                        a = (uint)input[i++] << (int)count;
                        b |= a;
                    }
                }

                var index = IndexTable[b >> 0x18];
                a = CharacterTable[index];
                var d = (b >> (int)(0x18 - a)) & BitMasks[a];
                var c = CharacterTable[index + 2 * d + 2];

                count += c;
                if (count > 0x20)
                {
                    var actual = (uint)result.Length - max;
                    output = new byte[actual];
                    Array.Copy(result, output, actual);
                    return;
                }

                max--;
                if (max == 0)
                    Array.Resize(ref result, result.Length * 2);

                a = CharacterTable[index + 2 * d + 1];
                result[j++] = (byte)a;

                b <<= c & 0xFF;
            }
        }

Usage Example

Пример #1
0
        public override void ThreadFunction()
        {
            m_socket = new TcpClient();
            //Initialize the game's data
            m_owner.InitializeGameData();

            Console.Write("{0}: [D2GS] Connecting to Game Server {1}:{2} .......", m_owner.Account, m_owner.GsIp, s_gsPort);
            try
            {
                m_socket.Connect(m_owner.GsIp, s_gsPort);
                m_stream = m_socket.GetStream();
                Console.WriteLine(" Connected");
            }
            catch
            {
                Console.WriteLine(" Failed To connect");
                return;
            }

            m_owner.ConnectedToGs = true;
            List <byte> buffer = new List <byte>();

            byte[] byteBuffer = new byte[4096];
            Int32  bytesRead  = 0;

            while (true)
            {
                try
                {
                    /*
                     * while (m_stream.DataAvailable)
                     * {
                     *  buffer.Add((byte)m_stream.ReadByte());
                     *  if(!m_stream.DataAvailable)
                     *      Console.WriteLine("{0}: [D2GS] Finished Reading to Buffer...", m_owner.Account);
                     * }
                     */
                    if (m_stream.DataAvailable)
                    {
                        bytesRead = m_stream.Read(byteBuffer, 0, byteBuffer.Length);
                        buffer.AddRange(new List <byte>(byteBuffer).GetRange(0, bytesRead));
                    }
                    else
                    {
                        if (!m_socket.Connected)
                        {
                            if (m_owner.ConnectedToGs)
                            {
                                m_owner.ConnectedToGs = false;
                                if (m_pingThread.IsAlive)
                                {
                                    m_pingThread.Join();
                                }
                                // Join threads
                            }
                            m_owner.Status = ClientlessBot.ClientStatus.STATUS_NOT_IN_GAME;
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    while (m_stream.DataAvailable)
                    {
                        buffer.Add((byte)m_stream.ReadByte());
                    }
                }
                catch
                {
                    //if (ClientlessBot.debugging)
                    Console.WriteLine("{0}: [D2GS] Disconnected from game server", m_owner.Account);
                    if (m_owner.ConnectedToGs)
                    {
                        m_owner.ConnectedToGs = false;
                        if (m_pingThread.IsAlive)
                        {
                            m_pingThread.Join();
                        }
                        // Join threads
                    }
                    m_owner.Status = ClientlessBot.ClientStatus.STATUS_NOT_IN_GAME;
                    break;
                }


                while (true)
                {
                    try
                    {
                        UInt16 receivedPacket = 0;
                        if (buffer.Count >= 2)
                        {
                            receivedPacket = BitConverter.ToUInt16(buffer.ToArray(), 0);
                        }
                        if (buffer.Count >= 2 && receivedPacket == (UInt16)0x01af)
                        {
                            if (ClientlessBot.debugging)
                            {
                                Console.WriteLine("{0}: [D2GS] Logging on to game server", m_owner.Account);
                            }

                            byte[] temp = { 0x50, 0xcc, 0x5d, 0xed,
                                            0xb6, 0x19, 0xa5, 0x91 };

                            Int32 pad = 16 - m_owner.Character.Length;

                            byte[] padding        = new byte[pad];
                            byte[] characterClass = { m_owner.ClassByte };
                            byte[] joinpacket     = BuildPacket(0x68, m_owner.GsHash, m_owner.GsToken, characterClass, BitConverter.GetBytes((UInt32)0xd), temp, zero, System.Text.Encoding.ASCII.GetBytes(m_owner.Character), padding);
                            Write(joinpacket);
                            //Console.WriteLine("{0}: [D2GS] Join packet sent to server", m_owner.Account);
                            buffer.RemoveRange(0, 2);
                        }

                        if (buffer.Count < 2 || (buffer[0] >= 0xF0 && buffer.Count < 3))
                        {
                            break;
                        }

                        Int32 headerSize;
                        Int32 length = Huffman.GetPacketSize(buffer, out headerSize);
                        if (length > buffer.Count)
                        {
                            break;
                        }

                        byte[] compressedPacket = buffer.GetRange(headerSize, length).ToArray();
                        buffer.RemoveRange(0, length + headerSize);


                        byte[] decompressedPacket;
                        Huffman.Decompress(compressedPacket, out decompressedPacket);
                        List <byte> packet = new List <byte>(decompressedPacket);
                        while (packet.Count != 0)
                        {
                            Int32 packetSize;
                            if (!GetPacketSize(packet, out packetSize))
                            {
                                Console.WriteLine("{0}: [D2GS] Failed to determine packet length", m_owner.Account);
                                break;
                            }
                            List <byte> actualPacket = new List <byte>(packet.GetRange(0, packetSize));
                            packet.RemoveRange(0, packetSize);

                            byte identifier = actualPacket[0];
                            DispatchPacket(identifier)(identifier, actualPacket);
                            m_owner.ReceivedGameServerPacket(actualPacket);
                        }
                    }
                    catch
                    {
                        m_owner.InGame        = false;
                        m_owner.ConnectedToGs = false;

                        Console.WriteLine("{0}: [D2GS] Leaving the game.", m_owner.Account);
                        Write(new byte[] { 0x69 });

                        Thread.Sleep(500);

                        if (m_pingThread.IsAlive)
                        {
                            m_pingThread.Join();
                        }
                        m_owner.Status = ClientlessBot.ClientStatus.STATUS_NOT_IN_GAME;
                        Kill();
                        return;
                    }
                }
            }
        }