Beispiel #1
0
        private void ReadVer2Info()
        {
            if (m_IntVersion != 0x200)
            {
                throw new GrfException();
            }

            byte[] buf = new byte[8], zbuf;
            int    len, len2;

            FileStream.Read(buf, 0, 8);
            len = EndianConverter.LittleEndian(BitConverter.ToInt32(buf, 0));

            if (0 == (len2 = EndianConverter.LittleEndian(BitConverter.ToInt32(buf, 4))))
            {
                return;
            }

            zbuf = new byte[len];
            FileStream.Read(zbuf, 0, len);

            using (IntPtrEx pBuffer = new IntPtrEx(ZlibStream.UncompressBuffer(zbuf)))
            {
                string  name = null;
                GrfItem item;

                try
                {
                    for (int i = 0, offset = 0; i < m_Items.Capacity; i++, offset += 0x11)
                    {
                        name = pBuffer.Read <string>(offset);
                        len  = name.Length + 1;

                        if (len >= GRF_NAMELEN)
                        {
                            throw new GrfException();
                        }

                        offset += len;

                        item = GrfItem.CreateV2
                               (
                            this,
                            name,
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset)),
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 4)),
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 8)),
                            (GrfFileFlags)pBuffer[offset + 0xC],
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 0xD)) + GRF_HEADER_FULL_LEN
                               );

                        m_Items.GrfAdd(item);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
Beispiel #2
0
        private void ReadVer1Info()
        {
            if (m_IntVersion > 0x103)
            {
                throw new GrfException();
            }

            int offset = (int)FileStream.Position;
            int len2, len = (int)FileStream.Length - offset;

            byte[] buffer = new byte[len], namebuf = new byte[GRF_NAMELEN];

            try
            {
                FileStream.Read(buffer, 0, len);

                string  name = null;
                GrfItem item;

                using (IntPtrEx pBuffer = new IntPtrEx(buffer))
                {
                    for (int i = offset = 0; i < m_Items.Capacity; i++)
                    {
                        len     = EndianConverter.LittleEndian(pBuffer.Read <int>(offset));
                        offset += 4;

                        if (m_IntVersion < 0x101)
                        {
                            len2 = pBuffer.Read <string>(offset).Length;
                            if (len2 >= GRF_NAMELEN)
                            {
                                throw new GrfException();
                            }

                            name = SwapNibbles(pBuffer + offset, len2);
                        }
                        else if (m_IntVersion < 0x104)
                        {
                            offset += 2;
                            len2    = len - 6;
                            if (len2 >= GRF_NAMELEN)
                            {
                                throw new GrfException();
                            }

                            SwapNibbles(namebuf, pBuffer + offset, len2);
                            name = GrfCrypt.DecryptNameVer1(namebuf, len2);

                            len -= 2;
                        }

                        offset += len;

                        item = GrfItem.CreateV1
                               (
                            this,
                            name,
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset)) - EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 8)) - 0x02CB,
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 4)) - 0x92CB,
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 8)),
                            (GrfFileFlags)pBuffer[offset + 0xC],
                            EndianConverter.LittleEndian(pBuffer.Read <int>(offset + 0xD)) + GRF_HEADER_FULL_LEN
                               );

                        m_Items.GrfAdd(item);

                        offset += 0x11;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Beispiel #3
0
        private void ReadHeader()
        {
            if (FileStream.CanWrite && FileStream.Length < 1)
            {
                byte[] zbuf        = ZlibStream.CompressBuffer(new byte[0]);
                byte[] zero        = new byte[4];
                int    zlen_le     = EndianConverter.LittleEndian(zbuf.Length),
                       zero_fcount = EndianConverter.LittleEndian(7),
                       create_ver  = EndianConverter.LittleEndian(m_IntVersion);

                FileStream.Write(Encoding.ASCII.GetBytes(GRF_HEADER), 0, GRF_HEADER_LEN);
                FileStream.Write(CryptWatermark, 0, CryptWatermark.Length);
                FileStream.Write(zero, 0, 4);
                FileStream.Write(zero, 0, 4);
                FileStream.Write(BitConverter.GetBytes(zero_fcount), 0, 4);
                FileStream.Write(BitConverter.GetBytes(create_ver), 0, 4);
                FileStream.Write(BitConverter.GetBytes(zlen_le), 0, 4);
                FileStream.Write(zero, 0, 4);
                FileStream.Write(zbuf, 0, zbuf.Length);

                FileStream.Seek(0, SeekOrigin.Begin);
            }

            byte[] buf = new byte[GRF_HEADER_FULL_LEN];
            FileStream.Read(buf, 0, buf.Length);

            if (buf[GRF_HEADER_LEN + 1] == 1)
            {
                m_AllowCrypt = true;
                // 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E
                for (byte i = 0; i < 0xF; i++)
                {
                    if (buf[GRF_HEADER_LEN + i] != i)
                    {
                        throw new GrfException();
                    }
                }
            }
            else if (buf[GRF_HEADER_LEN] == 0)
            {
                m_AllowCrypt = false;
                // 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                for (byte i = 0; i < 0xF; i++)
                {
                    if (buf[GRF_HEADER_LEN + i] != 0)
                    {
                        throw new GrfException();
                    }
                }
            }
            else
            {
                throw new GrfException();
            }

            using (IntPtrEx pBuffer = new IntPtrEx(buf))
            {
                m_Items = new GrfItemCollection(this);

                m_IntVersion     = EndianConverter.LittleEndian(pBuffer.Read <int>(GRF_HEADER_MID_LEN + 0xC));
                m_Items.Capacity = EndianConverter.LittleEndian(pBuffer.Read <int>(GRF_HEADER_MID_LEN + 8))
                                   - EndianConverter.LittleEndian(pBuffer.Read <int>(GRF_HEADER_MID_LEN + 4))
                                   - 7;

                FileStream.Seek(EndianConverter.LittleEndian(pBuffer.Read <int>(GRF_HEADER_MID_LEN) + GRF_HEADER_FULL_LEN), SeekOrigin.Begin);
            }

            switch (m_IntVersion & 0xFF00)
            {
            case 0x0200: ReadVer2Info(); break;

            case 0x0100: ReadVer1Info(); break;

            default: throw new GrfException();
            }
        }
Beispiel #4
0
        private void ReadVer2Info()
        {
            if (m_IntVersion != 0x200)
                throw new GrfException();

            byte[] buf = new byte[8], zbuf;
            int len, len2;

            FileStream.Read(buf, 0, 8);
            len = EndianConverter.LittleEndian(BitConverter.ToInt32(buf, 0));

            if (0 == (len2 = EndianConverter.LittleEndian(BitConverter.ToInt32(buf, 4))))
                return;

            zbuf = new byte[len];
            FileStream.Read(zbuf, 0, len);

            using (IntPtrEx pBuffer = new IntPtrEx(ZlibStream.UncompressBuffer(zbuf)))
            {
                string name = null;
                GrfItem item;

                try
                {
                    for (int i = 0, offset = 0; i < m_Items.Capacity; i++, offset += 0x11)
                    {
                        name = pBuffer.Read<string>(offset);
                        len = name.Length + 1;

                        if (len >= GRF_NAMELEN)
                            throw new GrfException();

                        offset += len;

                        item = GrfItem.CreateV2
                        (
                            this,
                            name,
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset)),
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 4)),
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 8)),
                            (GrfFileFlags)pBuffer[offset + 0xC],
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 0xD)) + GRF_HEADER_FULL_LEN
                        );

                        m_Items.GrfAdd(item);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
Beispiel #5
0
        private void ReadVer1Info()
        {
            if (m_IntVersion > 0x103)
                throw new GrfException();

            int offset = (int)FileStream.Position;
            int len2, len = (int)FileStream.Length - offset;
            byte[] buffer = new byte[len], namebuf = new byte[GRF_NAMELEN];

            try
            {
                FileStream.Read(buffer, 0, len);

                string name = null;
                GrfItem item;

                using (IntPtrEx pBuffer = new IntPtrEx(buffer))
                {
                    for (int i = offset = 0; i < m_Items.Capacity; i++)
                    {
                        len = EndianConverter.LittleEndian(pBuffer.Read<int>(offset));
                        offset += 4;

                        if (m_IntVersion < 0x101)
                        {
                            len2 = pBuffer.Read<string>(offset).Length;
                            if (len2 >= GRF_NAMELEN)
                                throw new GrfException();

                            name = SwapNibbles(pBuffer + offset, len2);
                        }
                        else if (m_IntVersion < 0x104)
                        {
                            offset += 2;
                            len2 = len - 6;
                            if (len2 >= GRF_NAMELEN)
                                throw new GrfException();

                            SwapNibbles(namebuf, pBuffer + offset, len2);
                            name = GrfCrypt.DecryptNameVer1(namebuf, len2);

                            len -= 2;
                        }

                        offset += len;

                        item = GrfItem.CreateV1
                        (
                            this,
                            name,
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset)) - EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 8)) - 0x02CB,
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 4)) - 0x92CB,
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 8)),
                            (GrfFileFlags)pBuffer[offset + 0xC],
                            EndianConverter.LittleEndian(pBuffer.Read<int>(offset + 0xD)) + GRF_HEADER_FULL_LEN
                        );

                        m_Items.GrfAdd(item);

                        offset += 0x11;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Beispiel #6
0
        private void ReadHeader()
        {
            if (FileStream.CanWrite && FileStream.Length < 1)
            {
                byte[] zbuf = ZlibStream.CompressBuffer(new byte[0]);
                byte[] zero = new byte[4];
                int zlen_le = EndianConverter.LittleEndian(zbuf.Length),
                    zero_fcount = EndianConverter.LittleEndian(7),
                    create_ver = EndianConverter.LittleEndian(m_IntVersion);

                FileStream.Write(Encoding.ASCII.GetBytes(GRF_HEADER), 0, GRF_HEADER_LEN);
                FileStream.Write(CryptWatermark, 0, CryptWatermark.Length);
                FileStream.Write(zero, 0, 4);
                FileStream.Write(zero, 0, 4);
                FileStream.Write(BitConverter.GetBytes(zero_fcount), 0, 4);
                FileStream.Write(BitConverter.GetBytes(create_ver), 0, 4);
                FileStream.Write(BitConverter.GetBytes(zlen_le), 0, 4);
                FileStream.Write(zero, 0, 4);
                FileStream.Write(zbuf, 0, zbuf.Length);

                FileStream.Seek(0, SeekOrigin.Begin);
            }

            byte[] buf = new byte[GRF_HEADER_FULL_LEN];
            FileStream.Read(buf, 0, buf.Length);

            if (buf[GRF_HEADER_LEN + 1] == 1)
            {
                m_AllowCrypt = true;
                // 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E
                for (byte i = 0; i < 0xF; i++)
                    if (buf[GRF_HEADER_LEN + i] != i)
                        throw new GrfException();
            }
            else if (buf[GRF_HEADER_LEN] == 0)
            {
                m_AllowCrypt = false;
                // 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                for (byte i = 0; i < 0xF; i++)
                    if (buf[GRF_HEADER_LEN + i] != 0)
                        throw new GrfException();
            }
            else
            {
                throw new GrfException();
            }

            using (IntPtrEx pBuffer = new IntPtrEx(buf))
            {
                m_Items = new GrfItemCollection(this);

                m_IntVersion = EndianConverter.LittleEndian(pBuffer.Read<int>(GRF_HEADER_MID_LEN + 0xC));
                m_Items.Capacity = EndianConverter.LittleEndian(pBuffer.Read<int>(GRF_HEADER_MID_LEN + 8))
                    - EndianConverter.LittleEndian(pBuffer.Read<int>(GRF_HEADER_MID_LEN + 4))
                    - 7;

                FileStream.Seek(EndianConverter.LittleEndian(pBuffer.Read<int>(GRF_HEADER_MID_LEN) + GRF_HEADER_FULL_LEN), SeekOrigin.Begin);
            }

            switch (m_IntVersion & 0xFF00)
            {
                case 0x0200: ReadVer2Info(); break;
                case 0x0100: ReadVer1Info(); break;
                default: throw new GrfException();
            }
        }