public override void Read(EndianBinaryReader reader, ISection section = null)
        {
            reader.PushBaseOffset();

            int signature = reader.ReadInt32();

            if (signature != 0x03505854)
            {
                reader.Endianness = Endianness = Endianness.Big;
                signature         = EndiannessHelper.Swap(signature);
            }

            if (signature != 0x03505854)
            {
                throw new InvalidDataException("Invalid signature (expected TXP with type 3)");
            }

            int textureCount            = reader.ReadInt32();
            int textureCountWithRubbish = reader.ReadInt32();

            Textures.Capacity = textureCount;

            for (int i = 0; i < textureCount; i++)
            {
                reader.ReadOffset(() => { Textures.Add(new Texture(reader)); });
            }

            reader.PopBaseOffset();
        }
        public override void Write(long value)
        {
            if (EndiannessHelper.Swap(value) != value)
            {
                SetBitArrayValue(Position - mBeginPosition, ( int )ValueType.Int64);
            }

            base.Write(value);
        }
예제 #3
0
        public override void Write(long value)
        {
            if (EndiannessHelper.Swap(value) != value)
            {
                mPositionMap[Position] = sizeof(long);
            }

            base.Write(value);
        }
 private void SwapHeader(ref BinaryHeader header)
 {
     EndiannessHelper.Swap(ref header.UserId);
     EndiannessHelper.Swap(ref header.FileSize);
     EndiannessHelper.Swap(ref header.Field0C);
     EndiannessHelper.Swap(ref header.RelocationTable.Offset);
     EndiannessHelper.Swap(ref header.RelocationTableSize);
     EndiannessHelper.Swap(ref header.DialogCount);
     EndiannessHelper.Swap(ref header.Field1E);
 }
예제 #5
0
        private int ReadSegmentLength()
        {
            int segmentLength = mReader.ReadInt32();

            if (segmentLength > mReader.BaseStreamLength || segmentLength < 0)
            {
                mReader.Endianness = mReader.Endianness == Endianness.LittleEndian
                    ? Endianness.BigEndian
                    : Endianness.LittleEndian;

                segmentLength = EndiannessHelper.Swap(segmentLength);
            }

            return(segmentLength);
        }
예제 #6
0
        public override void Read(EndianBinaryReader reader, ISection section = null)
        {
            if (section != null)
            {
                ReadModern();
            }
            else
            {
                ReadClassic();
            }

            void ReadClassic()
            {
                var offsets = new List <long>();

                // Try to determine endianness (apparently DT uses big endian string arrays)
                uint stringOffset = reader.ReadUInt32();

                if (stringOffset >= reader.Length)
                {
                    reader.Endianness = Endianness.Big;
                    stringOffset      = EndiannessHelper.Swap(stringOffset);
                }

                Endianness = reader.Endianness;

                do
                {
                    offsets.Add(stringOffset);
                    stringOffset = reader.ReadUInt32();
                } while (reader.Position < offsets[0] && stringOffset != 0);

                Strings.Capacity = offsets.Count;

                for (int i = 0; i < offsets.Count; i++)
                {
                    long offset = offsets[i];
                    reader.SeekBegin(offset);

                    string value = reader.ReadString(StringBinaryFormat.NullTerminated);
                    if (!string.IsNullOrEmpty(value))
                    {
                        Strings.Add(new StringEntry {
                            Value = value, Id = ( uint )i
                        });
                    }
                }
            }

            void ReadModern()
            {
                int count = reader.ReadInt32();

                reader.ReadOffset(() =>
                {
                    Strings.Capacity = count;

                    for (int i = 0; i < count; i++)
                    {
                        Strings.Add(new StringEntry
                        {
                            Value = reader.ReadStringOffset(StringBinaryFormat.NullTerminated),
                            Id    = reader.ReadUInt32()
                        });
                    }
                });
            }
        }
예제 #7
0
        private static bool IsValidArchiveVersion2And3(Stream stream, int entrySize)
        {
            // check stream length
            if (stream.Length <= 4 + entrySize)
            {
                return(false);
            }

            byte[] testData = new byte[4 + entrySize];
            stream.Read(testData, 0, 4 + entrySize);
            stream.Position = 0;

            int numOfFiles = BitConverter.ToInt32(testData, 0);

            // num of files sanity check
            if (numOfFiles > 1024 || numOfFiles < 1 || (numOfFiles * entrySize) > stream.Length)
            {
                numOfFiles = EndiannessHelper.Swap(numOfFiles);

                if (numOfFiles > 1024 || numOfFiles < 1 || (numOfFiles * entrySize) > stream.Length)
                {
                    return(false);
                }
            }

            // check if the name field is correct
            bool nameTerminated = false;

            for (int i = 0; i < entrySize - 4; i++)
            {
                if (testData[4 + i] == 0x00)
                {
                    if (i == 0)
                    {
                        return(false);
                    }

                    nameTerminated = true;
                }

                if (testData[4 + i] != 0x00 && nameTerminated)
                {
                    return(false);
                }
            }

            // first entry length sanity check
            int length = BitConverter.ToInt32(testData, entrySize);

            if (length >= stream.Length || length < 0)
            {
                length = EndiannessHelper.Swap(length);

                if (length >= stream.Length || length < 0)
                {
                    return(false);
                }
            }

            return(true);
        }