Decompressing Downloaded Patch Files

09/15/2015 18:25 magicanoo#1
Hello,

I'm trying to handle the files received from the downloadserver to the launcher. I have read Drew's guide here : [Only registered and activated users can see links. Click Here To Register...] and I understood most of the patching logic, but the implementation is not easy to understand because it's in C++.

I'm trying to implement the concept on a launcher using VSRO 1.88 downloadserver in C#. I reached the point where I could receive the data from the downloadserver and save it into a file. This is how it looks like:

[Only registered and activated users can see links. Click Here To Register...]

Method used :
Code:
System.IO.File.WriteAllText("textfile.txt", Utility.HexDump(packet.GetBytes()));
Does anyone have a clue about decompressing the data? I tried Zlib and 7z decompressors, but they don't work.

The code I used for decompression :
Code:
ZLibCompressor.DeCompress(packet_bytes);
Library used : [Only registered and activated users can see links. Click Here To Register...]


Cheers
09/15/2015 19:02 DaxterSoul#2
There is a [Only registered and activated users can see links. Click Here To Register...] and then there is [Only registered and activated users can see links. Click Here To Register...]...

Edit: Forget lzma. You where right, vSRO uses zlib. I will look into it.
Edit2: Solution found. I'm using the ZlibDll from [Only registered and activated users can see links. Click Here To Register...]
You need to read the first 4 bytes. It stores the uncompressed size. The rest of the file is compressed data.

Code:
    class Program
    {
        [DllImport("ZlibDll.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Decompress(byte[] compressed_buffer, int compressed_size, byte[] decompressed_buffer, ref int decompressed_size);

        [DllImport("ZlibDll.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Compress(byte[] decompressed_buffer, int decompressed_size, byte[] compressed_buffer, ref int compressed_size);

        enum ZLIB_RESULT
        {
            Z_OK = 0,
            Z_STREAM_END = 1,
            Z_NEED_DICT = 2,
            Z_ERRORNO = -1,
            Z_STREAM_ERROR = -2,
            Z_DATA_ERROR = -3,
            Z_MEM_ERROR = -4,
            Z_BUF_ERROR = -5,
            Z_VERSION_ERROR = -6
        }

        static void Main(string[] args)
        {
            using (var fileStream = new FileStream("testFile.txt.zlib", FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader reader = new BinaryReader(fileStream))
                {
                    int compressed_size = (int)reader.BaseStream.Length - 4;
                    int decompressed_size = reader.ReadInt32();

                    byte[] compressed_buffer = reader.ReadBytes(compressed_size);
                    byte[] decompressed_buffer = new byte[decompressed_size];

                    ZLIB_RESULT result = (ZLIB_RESULT)Program.Decompress(compressed_buffer, compressed_size, decompressed_buffer, ref decompressed_size);
                    if (result == ZLIB_RESULT.Z_OK)
                    {
                        string content = Encoding.Default.GetString(decompressed_buffer);
                        Console.WriteLine("Sucess.\n" + content);
                    }
                    else
                    {
                        Console.WriteLine("Error: {0}", result.ToString());
                    }
                }
            }
            Console.ReadLine();
        }
    }