Esempio n. 1
0
        public MSCabCompression(Stream stream)
        {
            this.stream = stream;

            var signature = stream.ReadASCII(4);
            if (signature != "MSCF")
                throw new InvalidDataException("Not a Microsoft CAB package!");

            stream.Position += 12;
            var filesOffset = stream.ReadUInt32();
            stream.Position += 6;
            var folderCount = stream.ReadUInt16();
            var fileCount = stream.ReadUInt16();
            if (stream.ReadUInt16() != 0)
                throw new InvalidDataException("Only plain packages (without reserved header space or prev/next archives) are supported!");

            stream.Position += 4;

            folders = new CabFolder[folderCount];
            for (var i = 0; i < folderCount; i++)
            {
                folders[i] = new CabFolder(stream);
                if (folders[i].CompressionType != 1)
                    throw new InvalidDataException("Compression type is not supported");
            }

            files = new CabFile[fileCount];
            stream.Seek(filesOffset, SeekOrigin.Begin);
            for (var i = 0; i < fileCount; i++)
                files[i] = new CabFile(stream);
        }
Esempio n. 2
0
		public MixFile(string filename, PackageHashType type, int priority)
		{
			this.filename = filename;
			this.priority = priority;
			this.type = type;
			s = GlobalFileSystem.Open(filename);

			// Detect format type
			s.Seek(0, SeekOrigin.Begin);
			var isCncMix = s.ReadUInt16() != 0;

			// The C&C mix format doesn't contain any flags or encryption
			var isEncrypted = false;
			if (!isCncMix)
				isEncrypted = (s.ReadUInt16() & 0x2) != 0;

			List<PackageEntry> entries;
			if (isEncrypted)
			{
				long unused;
				entries = ParseHeader(DecryptHeader(s, 4, out dataStart), 0, out unused);
			}
			else
				entries = ParseHeader(s, isCncMix ? 0 : 4, out dataStart);

			index = entries.ToDictionaryWithConflictLog(x => x.Hash,
				"{0} ({1} format, Encrypted: {2}, DataStart: {3})".F(filename, (isCncMix ? "C&C" : "RA/TS/RA2"), isEncrypted, dataStart),
				null, x => "(offs={0}, len={1})".F(x.Offset, x.Length)
			);
		}
Esempio n. 3
0
        public ShpReader(Stream stream)
        {
            imageCount = stream.ReadUInt16();
            stream.Position += 4;
            var width = stream.ReadUInt16();
            var height = stream.ReadUInt16();
            Size = new Size(width, height);

            stream.Position += 4;
            for (var i = 0; i < imageCount; i++)
                headers.Add(new ImageHeader(stream, this));

            // Skip eof and zero headers
            stream.Position += 16;

            var offsets = headers.ToDictionary(h => h.FileOffset, h => h);
            for (var i = 0; i < imageCount; i++)
            {
                var h = headers[i];
                if (h.Format == Format.Format20)
                    h.RefImage = headers[i - 1];

                else if (h.Format == Format.Format40 && !offsets.TryGetValue(h.RefOffset, out h.RefImage))
                    throw new InvalidDataException("Reference doesnt point to image data {0}->{1}".F(h.FileOffset, h.RefOffset));
            }

            foreach (var h in headers)
                Decompress(stream, h);

            spriteFrames = Exts.Lazy(() => headers.Cast<ISpriteFrame>());
        }
Esempio n. 4
0
		public ShpReader(Stream stream)
		{
			imageCount = stream.ReadUInt16();
			stream.Position += 4;
			var width = stream.ReadUInt16();
			var height = stream.ReadUInt16();
			Size = new Size(width, height);

			stream.Position += 4;
			var headers = new ImageHeader[imageCount];
			Frames = headers.AsReadOnly();
			for (var i = 0; i < headers.Length; i++)
				headers[i] = new ImageHeader(stream, this);

			// Skip eof and zero headers
			stream.Position += 16;

			var offsets = headers.ToDictionary(h => h.FileOffset, h => h);
			for (var i = 0; i < imageCount; i++)
			{
				var h = headers[i];
				if (h.Format == Format.Format20)
					h.RefImage = headers[i - 1];
				else if (h.Format == Format.Format40 && !offsets.TryGetValue(h.RefOffset, out h.RefImage))
					throw new InvalidDataException("Reference doesnt point to image data {0}->{1}".F(h.FileOffset, h.RefOffset));
			}

			shpBytesFileOffset = stream.Position;
			shpBytes = stream.ReadBytes((int)(stream.Length - stream.Position));

			foreach (var h in headers)
				Decompress(h);
		}
Esempio n. 5
0
        TmpTDFrame[] ParseFrames(Stream s)
        {
            var start = s.Position;
            var width = s.ReadUInt16();
            var height = s.ReadUInt16();
            var size = new Size(width, height);

            s.Position += 8;
            var imgStart = s.ReadUInt32();
            s.Position += 8;
            var indexEnd = s.ReadInt32();
            var indexStart = s.ReadInt32();

            s.Position = indexStart;
            var count = indexEnd - indexStart;
            var tiles = new TmpTDFrame[count];
            var tilesIndex = 0;
            foreach (var b in s.ReadBytes(count))
            {
                if (b != 255)
                {
                    s.Position = imgStart + b * width * height;
                    tiles[tilesIndex++] = new TmpTDFrame(s.ReadBytes(width * height), size);
                }
                else
                    tiles[tilesIndex++] = new TmpTDFrame(null, size);
            }

            s.Position = start;
            return tiles;
        }
Esempio n. 6
0
        public MixFile(FileSystem context, string filename)
        {
            Name = filename;
            this.context = context;

            s = context.Open(filename);
            try
            {
                // Detect format type
                var isCncMix = s.ReadUInt16() != 0;

                // The C&C mix format doesn't contain any flags or encryption
                var isEncrypted = false;
                if (!isCncMix)
                    isEncrypted = (s.ReadUInt16() & 0x2) != 0;

                List<PackageEntry> entries;
                if (isEncrypted)
                {
                    long unused;
                    entries = ParseHeader(DecryptHeader(s, 4, out dataStart), 0, out unused);
                }
                else
                    entries = ParseHeader(s, isCncMix ? 0 : 4, out dataStart);

                index = ParseIndex(entries.ToDictionaryWithConflictLog(x => x.Hash,
                    "{0} ({1} format, Encrypted: {2}, DataStart: {3})".F(filename, isCncMix ? "C&C" : "RA/TS/RA2", isEncrypted, dataStart),
                    null, x => "(offs={0}, len={1})".F(x.Offset, x.Length)));
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }
        }
        public Container(Stream stream)
        {
            UInt16 stringLength = stream.ReadUInt16();
            Name = stream.ReadAsciiString(stringLength);
            ContainerType = stream.ReadUInt8();
            Flags = (ContainerFlags)stream.ReadUInt16();
            PrimitiveCount = stream.ReadUInt16();
            PackfileBaseOffset = stream.ReadUInt32();
            CompressionType = stream.ReadUInt8();
            stringLength = stream.ReadUInt16();
            StubContainerParentName = stream.ReadAsciiString(stringLength);
            Int32 auxDataSize = stream.ReadInt32();
            AuxData = new byte[auxDataSize];
            stream.Read(AuxData, 0, auxDataSize);
            TotalCompressedPackfileReadSize = stream.ReadInt32();

            Primitives = new List<Primitive>();
            PrimitiveSizes = new List<WriteTimeSizes>();

            for (UInt16 i = 0; i < PrimitiveCount; i++)
            {
                var sizes = stream.ReadStruct<WriteTimeSizes>();
                PrimitiveSizes.Add(sizes);
            }

            for (UInt16 i = 0; i < PrimitiveCount; i++)
            {
                Primitive primitive = new Primitive(stream);
                Primitives.Add(primitive);
            }
        }
Esempio n. 8
0
        bool IsWsaD2(Stream s)
        {
            if (s.Length < 10)
                return false;

            var start = s.Position;

            numTiles = s.ReadUInt16();
            tileWidth = s.ReadUInt16();
            tileHeight = s.ReadUInt16();
            Delta = s.ReadUInt32();

            offsets = new uint[numTiles + 1];
            for (var i = 0; i <= numTiles; i++)
                offsets[i] = s.ReadUInt32();

            s.Position = start;

            //if (offsets[numTiles] < s.Length)
            //	return false;

            if (offsets[0] == 0)
            {
                numTiles -= 1;
                for (var i = 1; i <= numTiles; i++)
                    offsets[i - 1] = offsets[i];
            }

            return true;
        }
Esempio n. 9
0
 public static Chunk Read(Stream s)
 {
     Chunk c;
     c.CompressedSize = s.ReadUInt16();
     c.OutputSize = s.ReadUInt16();
     if (0xdeaf != s.ReadUInt32())
         throw new InvalidDataException("Chunk header is bogus");
     return c;
 }
Esempio n. 10
0
			public ImageHeader(Stream stream, ShpReader reader)
			{
				this.reader = reader;
				var data = stream.ReadUInt32();
				FileOffset = data & 0xffffff;
				Format = (Format)(data >> 24);

				RefOffset = stream.ReadUInt16();
				RefFormat = (Format)stream.ReadUInt16();
			}
Esempio n. 11
0
        public InstallShieldPackage(FileSystem context, string filename)
        {
            Name = filename;

            s = context.Open(filename);
            try
            {
                // Parse package header
                var signature = s.ReadUInt32();
                if (signature != 0x8C655D13)
                    throw new InvalidDataException("Not an Installshield package");

                s.Position += 8;
                /*var FileCount = */s.ReadUInt16();
                s.Position += 4;
                /*var ArchiveSize = */s.ReadUInt32();
                s.Position += 19;
                var tocAddress = s.ReadInt32();
                s.Position += 4;
                var dirCount = s.ReadUInt16();

                // Parse the directory list
                s.Position = tocAddress;

                // Parse directories
                var directories = new Dictionary<string, uint>();
                for (var i = 0; i < dirCount; i++)
                {
                    // Parse directory header
                    var fileCount = s.ReadUInt16();
                    var chunkSize = s.ReadUInt16();
                    var nameLength = s.ReadUInt16();
                    var dirName = s.ReadASCII(nameLength);

                    // Skip to the end of the chunk
                    s.ReadBytes(chunkSize - nameLength - 6);
                    directories.Add(dirName, fileCount);
                }

                // Parse files
                foreach (var dir in directories)
                    for (var i = 0; i < dir.Value; i++)
                        ParseFile(s, dir.Key);
            }
            catch
            {
                Dispose();
                throw;
            }
        }
Esempio n. 12
0
			public ShpD2Frame(Stream s)
			{
				var flags = (FormatFlags)s.ReadUInt16();
				s.Position += 1;
				var width = s.ReadUInt16();
				var height = s.ReadUInt8();
				Size = new Size(width, height);

				// Subtract header size
				var dataLeft = s.ReadUInt16() - 10;
				var dataSize = s.ReadUInt16();

				byte[] table;
				if ((flags & FormatFlags.PaletteTable) != 0)
				{
					var n = (flags & FormatFlags.VariableLengthTable) != 0 ? s.ReadUInt8() : (byte)16;
					table = new byte[n];
					for (var i = 0; i < n; i++)
						table[i] = s.ReadUInt8();

					dataLeft -= n;
				}
				else
				{
					table = new byte[256];
					for (var i = 0; i < 256; i++)
						table[i] = (byte)i;
					table[1] = 0x7f;
					table[2] = 0x7e;
					table[3] = 0x7d;
					table[4] = 0x7c;
				}

				Data = new byte[width * height];

				// Decode image data
				var compressed = s.ReadBytes(dataLeft);
				if ((flags & FormatFlags.SkipFormat80) == 0)
				{
					var temp = new byte[dataSize];
					Format80.DecodeInto(compressed, temp);
					compressed = temp;
				}

				Format2.DecodeInto(compressed, Data, 0);

				// Lookup values in lookup table
				for (var j = 0; j < Data.Length; j++)
					Data[j] = table[Data[j]];
			}
        private string ReadStringFromPositionAndReset( Stream s, uint stringPosition )
        {
            long pos = s.Position;
            s.Position = stringPosition;

            StringBuilder sb = new StringBuilder();
            byte[] buffer = new byte[2];

            int b = s.ReadByte();
            while ( b != 0 && b != -1 ) {
                if ( b == 0x01 ) {
                    sb.Append( '\n' );
                } else if ( b == 0x02 ) {
                    if ( s.ReadByte() != 0x00 ) { throw new Exception( "control code 0x02 not followed by a null byte!" ); }
                    sb.AppendFormat( "<Voice: {0:x4}>", s.ReadUInt16() );
                } else if ( b == 0x03 ) {
                    throw new Exception( "unknown control code 0x03" );
                } else if ( b == 0x04 ) {
                    throw new Exception( "unknown control code 0x04" );
                } else if ( ( b > 0x04 && b <= 0x80 ) || ( b >= 0xA0 && b <= 0xDF ) ) {
                    // is a single byte
                    buffer[0] = (byte)b;
                    sb.Append( Util.ShiftJISEncoding.GetString( buffer, 0, 1 ) );
                } else {
                    // is two bytes
                    buffer[0] = (byte)b;
                    buffer[1] = (byte)s.ReadByte();
                    sb.Append( Util.ShiftJISEncoding.GetString( buffer ) );
                }
                b = s.ReadByte();
            }

            s.Position = pos;
            return sb.ToString();
        }
Esempio n. 14
0
		static bool IsShpTD(Stream s)
		{
			var start = s.Position;

			// First word is the image count
			var imageCount = s.ReadUInt16();
			if (imageCount == 0)
			{
				s.Position = start;
				return false;
			}

			// Last offset should point to the end of file
			var finalOffset = start + 14 + 8 * imageCount;
			if (finalOffset > s.Length)
			{
				s.Position = start;
				return false;
			}

			s.Position = finalOffset;
			var eof = s.ReadUInt32();
			if (eof != s.Length)
			{
				s.Position = start;
				return false;
			}

			// Check the format flag on the first frame
			s.Position = start + 17;
			var b = s.ReadUInt8();

			s.Position = start;
			return b == 0x20 || b == 0x40 || b == 0x80;
		}
Esempio n. 15
0
        ShpTSFrame[] ParseFrames(Stream s)
        {
            var start = s.Position;

            s.ReadUInt16();
            var width = s.ReadUInt16();
            var height = s.ReadUInt16();
            var size = new Size(width, height);
            var frameCount = s.ReadUInt16();

            var frames = new ShpTSFrame[frameCount];
            for (var i = 0; i < frames.Length; i++)
                frames[i] = new ShpTSFrame(s, size);

            s.Position = start;
            return frames;
        }
Esempio n. 16
0
        bool IsTmpRA(Stream s)
        {
            var start = s.Position;

            s.Position += 20;
            var a = s.ReadUInt32();
            s.Position += 2;
            var b = s.ReadUInt16();

            s.Position = start;
            return a == 0 && b == 0x2c73;
        }
Esempio n. 17
0
        public ShpD2Reader(Stream s)
        {
            var imageCount = s.ReadUInt16();

            // Last offset is pointer to end of file.
            var offsets = new uint[imageCount + 1];
            var temp = s.ReadUInt32();

            // If fourth byte in file is non-zero, the offsets are two bytes each.
            var twoByteOffset = (temp & 0xFF0000) > 0;
            s.Position = 2;

            for (var i = 0; i < imageCount + 1; i++)
                offsets[i] = (twoByteOffset ? s.ReadUInt16() : s.ReadUInt32()) + 2;

            for (var i = 0; i < imageCount; i++)
            {
                s.Position = offsets[i];
                frames.Add(new Frame(s));
            }
        }
Esempio n. 18
0
		static List<PackageEntry> ParseHeader(Stream s, long offset, out long headerEnd)
		{
			s.Seek(offset, SeekOrigin.Begin);
			var numFiles = s.ReadUInt16();
			/*uint dataSize = */s.ReadUInt32();

			var items = new List<PackageEntry>();
			for (var i = 0; i < numFiles; i++)
				items.Add(new PackageEntry(s));

			headerEnd = offset + 6 + numFiles*PackageEntry.Size;
			return items;
		}
Esempio n. 19
0
        public BinaryDataHeader(Stream s, int2 expectedSize)
        {
            Format = s.ReadUInt8();
            var width = s.ReadUInt16();
            var height = s.ReadUInt16();
            if (width != expectedSize.X || height != expectedSize.Y)
                throw new InvalidDataException("Invalid tile data");

            if (Format == 1)
            {
                TilesOffset = 5;
                HeightsOffset = 0;
                ResourcesOffset = (uint)(3 * width * height + 5);
            }
            else if (Format == 2)
            {
                TilesOffset = s.ReadUInt32();
                HeightsOffset = s.ReadUInt32();
                ResourcesOffset = s.ReadUInt32();
            }
            else
                throw new InvalidDataException("Unknown binary map format '{0}'".F(Format));
        }
Esempio n. 20
0
        bool IsShpD2(Stream s)
        {
            var start = s.Position;

            // First word is the image count
            var imageCount = s.ReadUInt16();
            if (imageCount == 0)
            {
                s.Position = start;
                return false;
            }

            // Test for two vs four byte offset
            var testOffset = s.ReadUInt32();
            var offsetSize = (testOffset & 0xFF0000) > 0 ? 2 : 4;

            // Last offset should point to the end of file
            var finalOffset = start + 2 + offsetSize * imageCount;
            if (finalOffset > s.Length)
            {
                s.Position = start;
                return false;
            }

            s.Position = finalOffset;
            var eof = offsetSize == 2 ? s.ReadUInt16() : s.ReadUInt32();
            if (eof + 2 != s.Length)
            {
                s.Position = start;
                return false;
            }

            // Check the format flag on the first frame
            var b = s.ReadUInt16();
            s.Position = start;
            return b == 5 || b <= 3;
        }
        public Stream2File(Stream stream)
        {
            Header = stream.ReadStruct<ContainerFileHeader>();

            uint allocatorTypeCount = stream.ReadUInt32();
            for (uint i = 0; i < allocatorTypeCount; i++)
            {
                UInt16 stringLength = stream.ReadUInt16();
                string name = stream.ReadAsciiString(stringLength);
                byte id = stream.ReadUInt8();
                AllocatorTypes.Add(id, name);
            }

            uint primitiveTypeCount = stream.ReadUInt32();
            for (uint i = 0; i < primitiveTypeCount; i++)
            {
                UInt16 stringLength = stream.ReadUInt16();
                string name = stream.ReadAsciiString(stringLength);
                byte id = stream.ReadUInt8();
                PrimitiveTypes.Add(id, name);
            }

            uint containerTypeCount = stream.ReadUInt32();
            for (uint i = 0; i < containerTypeCount; i++)
            {
                UInt16 stringLength = stream.ReadUInt16();
                string name = stream.ReadAsciiString(stringLength);
                byte id = stream.ReadUInt8();
                ContainerTypes.Add(id, name);
            }

            for (uint i = 0; i < Header.NumContainers; i++)
            {
                Container container = new Container(stream);
                Containers.Add(container);
            }
        }
Esempio n. 22
0
        bool IsShpTS(Stream s)
        {
            var start = s.Position;

            // First word is zero
            if (s.ReadUInt16() != 0)
            {
                s.Position = start;
                return false;
            }

            // Sanity Check the image count
            s.Position += 4;
            var imageCount = s.ReadUInt16();
            if (s.Position + 24 * imageCount > s.Length)
            {
                s.Position = start;
                return false;
            }

            // Check the size and format flag
            // Some files define bogus frames, so loop until we find a valid one
            s.Position += 4;
            ushort w, h, f = 0;
            byte type;
            do
            {
                w = s.ReadUInt16();
                h = s.ReadUInt16();
                type = s.ReadUInt8();
            }
            while (w == 0 && h == 0 && f++ < imageCount);

            s.Position = start;
            return type < 4;
        }
Esempio n. 23
0
        void ParseFile(Stream s, string dirName)
        {
            s.Position += 7;
            var compressedSize = s.ReadUInt32();

            s.Position += 12;
            var chunkSize = s.ReadUInt16();

            s.Position += 4;
            var nameLength = s.ReadByte();
            var fileName   = dirName + "\\" + s.ReadASCII(nameLength);

            // Use index syntax to overwrite any duplicate entries with the last value
            index[fileName]  = new Entry(accumulatedData, compressedSize);
            accumulatedData += compressedSize;

            // Skip to the end of the chunk
            s.Position += chunkSize - nameLength - 30;
        }
Esempio n. 24
0
        public ValueSerializer GetDeserializerByManifest([NotNull] Stream stream, [NotNull] DeserializerSession session)
        {
            var first = stream.ReadByte();

            if (first <= 250)
            {
                return(_deserializerLookup[first]);
            }
            switch (first)
            {
            case ConsistentArraySerializer.Manifest:
                return(ConsistentArraySerializer.Instance);

            case ObjectReferenceSerializer.Manifest:
                return(ObjectReferenceSerializer.Instance);

            case ObjectSerializer.ManifestFull:
            {
                var type = TypeEx.GetTypeFromManifestFull(stream, session);
                return(GetCustomDeserializer(type));
            }

            case ObjectSerializer.ManifestVersion:
            {
                var type = TypeEx.GetTypeFromManifestVersion(stream, session);
                return(GetCustomDeserializer(type));
            }

            case ObjectSerializer.ManifestIndex:
            {
                var typeId = (int)stream.ReadUInt16(session);
                if (typeId < _knownValueSerializers.Length)
                {
                    return(_knownValueSerializers[typeId]);
                }
                var type = TypeEx.GetTypeFromManifestIndex(typeId, session);
                return(GetCustomDeserializer(type));
            }

            default:
                throw new NotSupportedException("Unknown manifest value");
            }
        }
Esempio n. 25
0
        bool IsWsa(Stream s)
        {
            var start = s.Position;

            var frames = s.ReadUInt16();

            if (frames <= 1)             // TODO: find a better way to differentiate .shp icons
            {
                return(false);
            }

            var x      = s.ReadUInt16();
            var y      = s.ReadUInt16();
            var width  = s.ReadUInt16();
            var height = s.ReadUInt16();

            if (width <= 0 || height <= 0)
            {
                return(false);
            }

            var delta = s.ReadUInt16() + 37;

            var flags = s.ReadUInt16();

            var offsets = new uint[frames + 2];

            for (var i = 0; i < offsets.Length; i++)
            {
                offsets[i] = s.ReadUInt32();
            }

            if (flags == 1)
            {
                var palette = StreamExts.ReadBytes(s, 768);
                for (var i = 0; i < offsets.Length; i++)
                {
                    offsets[i] += 768;
                }
            }

            s.Position = start;

            return(s.Length == offsets.Last());
        }
Esempio n. 26
0
        public void Read(Stream stream)
        {
            var magic = new byte[MAGIC.Length];

            stream.Read(magic);
            if (!MAGIC.SequenceEqual(magic))
            {
                throw new BadImageFormatException($"Magic value ({magic.ToByteString()}) is not present for a PE32 header");
            }

            mMajorLinkerVersion      = (byte)stream.ReadByte();
            mMinorLinkerVersion      = (byte)stream.ReadByte();
            mSizeOfCode              = stream.ReadUInt32();
            mSizeOfInitializedData   = stream.ReadUInt32();
            mSizeOfUninitializedData = stream.ReadUInt32();
            mAddressOfEntryPoint     = stream.ReadUInt32();
            mBaseOfCode              = stream.ReadUInt32();
            mBaseOfData              = stream.ReadUInt32();
            mImageBase                   = stream.ReadUInt32();
            mSectionAlignment            = stream.ReadUInt32();
            mFileAlignment               = stream.ReadUInt32();
            mMajorOperatingSystemVersion = stream.ReadUInt16();
            mMinorOperatingSystemVersion = stream.ReadUInt16();
            mMajorImageVersion           = stream.ReadUInt16();
            mMinorImageVersion           = stream.ReadUInt16();
            mMajorSubsystemVersion       = stream.ReadUInt16();
            mMinorSubsystemVersion       = stream.ReadUInt16();
            mWin32VersionValue           = stream.ReadUInt32();
            mSizeOfImage                 = stream.ReadUInt32();
            mSizeOfHeaders               = stream.ReadUInt32();
            mCheckSum            = stream.ReadUInt32();
            mSubsystem           = stream.ReadUInt16();
            mDllCharacteristics  = stream.ReadUInt16();
            mSizeOfStackReserve  = stream.ReadUInt32();
            mSizeOfStackCommit   = stream.ReadUInt32();
            mSizeOfHeapReserve   = stream.ReadUInt32();
            mSizeOfHeapCommit    = stream.ReadUInt32();
            mLoaderFlags         = stream.ReadUInt32();
            mNumberOfRvaAndSizes = stream.ReadUInt32();
        }
Esempio n. 27
0
        public ShpTSReader(Stream stream)
        {
            stream.ReadUInt16();
            var width = stream.ReadUInt16();
            var height = stream.ReadUInt16();
            var size = new Size(width, height);
            var frameCount = stream.ReadUInt16();

            for (var i = 0; i < frameCount; i++)
                frames.Add(new FrameHeader(stream, size));

            for (var i = 0; i < frameCount; i++)
            {
                var f = frames[i];
                if (f.FileOffset == 0)
                    continue;

                stream.Position = f.FileOffset;

                // Uncompressed
                if (f.Format == 1 || f.Format == 0)
                    f.Data = stream.ReadBytes(f.Size.Width * f.Size.Height);

                // Uncompressed scanlines
                else if (f.Format == 2)
                {
                    f.Data = new byte[f.Size.Width * f.Size.Height];
                    for (var j = 0; j < f.Size.Height; j++)
                    {
                        var length = stream.ReadUInt16() - 2;
                        stream.Read(f.Data, f.Size.Width * j, length);
                    }
                }

                // RLE-zero compressed scanlines
                else if (f.Format == 3)
                {
                    f.Data = new byte[f.Size.Width * f.Size.Height];

                    for (var j = 0; j < f.Size.Height; j++)
                    {
                        var length = stream.ReadUInt16() - 2;
                        Format2.DecodeInto(stream.ReadBytes(length), f.Data, j * f.Size.Width);
                    }
                }
            }

            spriteFrames = Exts.Lazy(() => frames.Cast<ISpriteFrame>());
        }
Esempio n. 28
0
        /// <summary>
        /// 读取一个表示数据长度的数据
        /// </summary>
        /// <param name="stream">从中读取的流</param>
        /// <returns>返回读取到的长度信息数据</returns>
        /// <exception cref="InvalidDataException">当遇到文件流的末尾时引发</exception>
        public static int ReadLengthData(this Stream stream)
        {
            var len = stream.ReadByte();

            if (len < 0)
            {
                throw new InvalidDataException("遇到文件尾");
            }

            if (len == 254)
            {
                len = stream.ReadUInt16();
            }
            else if (len == 255)
            {
                len = stream.ReadInt32();
            }

            return(len);
        }
Esempio n. 29
0
        public static float SoundLength(Stream s)
        {
            var sampleRate = s.ReadUInt16();

            /*var dataSize = */ s.ReadInt32();
            var outputSize = s.ReadInt32();
            var flags      = (SoundFlags)s.ReadByte();

            var samples = outputSize;

            if (0 != (flags & SoundFlags.Stereo))
            {
                samples /= 2;
            }
            if (0 != (flags & SoundFlags._16Bit))
            {
                samples /= 2;
            }
            return(samples / sampleRate);
        }
Esempio n. 30
0
        private static object ReadPrimitiveData(Stream s, SerializeTypeEnum type)
        {
            switch (type)
            {
            case SerializeTypeEnum.Bool:
                return(s.ReadBool());

            case SerializeTypeEnum.Byte:
                return((byte)s.ReadByte());

            case SerializeTypeEnum.Double:
                return(s.ReadDouble());

            case SerializeTypeEnum.Float:
                return(s.ReadFloat());

            case SerializeTypeEnum.Int16:
                return(s.ReadInt16());

            case SerializeTypeEnum.Int32:
                return(s.ReadInt32());

            case SerializeTypeEnum.Int64:
                return(s.ReadInt64());

            case SerializeTypeEnum.UInt16:
                return(s.ReadUInt16());

            case SerializeTypeEnum.UInt32:
                return(s.ReadUInt32());

            case SerializeTypeEnum.UInt64:
                return(s.ReadUInt64());

            case SerializeTypeEnum.String:
                return(s.ReadString(Encoding.ASCII));

            default:
                throw new Exception();
            }
        }
            public void CopyTo(Stream output, Action <int> onProgress)
            {
                if (file.Flags.HasFlag(CABFlags.FileCompressed))
                {
                    var inf    = new Inflater(true);
                    var buffer = new byte[165535];
                    do
                    {
                        var bytesToExtract = currentVolume.ReadUInt16();
                        remainingInArchive -= 2;
                        toExtract          -= 2;
                        inf.SetInput(GetBytes(bytesToExtract));
                        toExtract -= bytesToExtract;
                        while (!inf.IsNeedingInput)
                        {
                            if (onProgress != null)
                            {
                                onProgress((int)(100 * output.Position / file.ExpandedSize));
                            }

                            var inflated = inf.Inflate(buffer);
                            output.Write(buffer, 0, inflated);
                        }

                        inf.Reset();
                    } while (toExtract > 0);
                }
                else
                {
                    do
                    {
                        if (onProgress != null)
                        {
                            onProgress((int)(100 * output.Position / file.ExpandedSize));
                        }

                        toExtract -= remainingInArchive;
                        output.Write(GetBytes(remainingInArchive), 0, (int)remainingInArchive);
                    } while (toExtract > 0);
                }
            }
Esempio n. 32
0
        public VoiceTable(Stream s, EndianUtils.Endianness e = EndianUtils.Endianness.LittleEndian)
        {
            s.Position = 0;
            ushort count = s.ReadUInt16(e);

            if (s.PeekUInt32(e) == 1)
            {
                // CS2 has something odd I don't fully understand at the start of the file, try to skip past that
                UnknownStartOfFileData = s.ReadUInt8Array(0xe);
            }
            else
            {
                UnknownStartOfFileData = null;
            }

            Entries = new List <VoiceTableEntry>();
            for (int i = 0; i < count; ++i)
            {
                Entries.Add(new VoiceTableEntry(s, e));
            }
        }
Esempio n. 33
0
        public static bool LoadSound(Stream s, out Func <Stream> result, out int sampleRate, out int sampleBits, out int channels)
        {
            result = null;
            var startPosition = s.Position;

            try
            {
                sampleRate = s.ReadUInt16();
                var dataSize   = s.ReadInt32();
                var outputSize = s.ReadInt32();
                var readFlag   = s.ReadByte();
                sampleBits = (readFlag & (int)SoundFlags._16Bit) == 0 ? 8 : 16;
                channels   = (readFlag & (int)SoundFlags.Stereo) == 0 ? 1 : 2;

                var readFormat = s.ReadByte();
                if (!Enum.IsDefined(typeof(SoundFormat), readFormat))
                {
                    return(false);
                }

                if (readFormat == (int)SoundFormat.WestwoodCompressed)
                {
                    throw new NotImplementedException();
                }

                var offsetPosition = s.Position;

                result = () =>
                {
                    var audioStream = SegmentStream.CreateWithoutOwningStream(s, offsetPosition, (int)(s.Length - offsetPosition));
                    return(new AudStream(audioStream, outputSize, dataSize));
                };
            }
            finally
            {
                s.Position = startPosition;
            }

            return(true);
        }
Esempio n. 34
0
        void SkipProperty()
        {
            uint raw  = Stream.ReadUInt8();
            var  type = (SirenTypeId)(raw & 0x1f);

            raw >>= 5;

            if (raw < 6)
            {
            }
            else if (raw == 6)
            {
                Stream.ReadUInt8();
            }
            else
            {
                Stream.ReadUInt16();
            }


            SkipPropertyHelper(type);
        }
Esempio n. 35
0
        public void ApplyPatch(long offset, ulong value, ulong mask, byte patchSize, Endianness endianness)
        {
            try
            {
                Stream.Position = offset;

                ulong current = 0;

                switch (patchSize)
                {
                case 8: current = (ulong)Stream.ReadByte(); break;

                case 16: current = (ulong)Stream.ReadUInt16(endianness); break;

                case 32: current = (ulong)Stream.ReadUInt32(endianness); break;

                case 64: current = (ulong)Stream.ReadUInt64(endianness); break;
                }

                Stream.Position = offset;
                current         = (current & ~mask) | value;

                // Apply the patch
                switch (patchSize)
                {
                case 8: Stream.WriteByte((byte)current); break;

                case 16: Stream.Write((ushort)current, endianness); break;

                case 32: Stream.Write((uint)current, endianness); break;

                case 64: Stream.Write((ulong)current, endianness); break;
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
Esempio n. 36
0
        public static byte[] LoadSound(Stream s)
        {
            /*var sampleRate =*/ s.ReadUInt16();
            var dataSize   = s.ReadInt32();
            var outputSize = s.ReadInt32();

            /*var flags = (SoundFlags)*/ s.ReadByte();
            /*var format = (SoundFormat)*/ s.ReadByte();

            var output        = new byte[outputSize];
            var offset        = 0;
            var index         = 0;
            var currentSample = 0;

            while (dataSize > 0)
            {
                var chunk = Chunk.Read(s);
                for (var n = 0; n < chunk.CompressedSize; n++)
                {
                    var b = s.ReadUInt8();

                    var t = DecodeSample(b, ref index, ref currentSample);
                    output[offset++] = (byte)t;
                    output[offset++] = (byte)(t >> 8);

                    if (offset < outputSize)
                    {
                        /* possible that only half of the final byte is used! */
                        t = DecodeSample((byte)(b >> 4), ref index, ref currentSample);
                        output[offset++] = (byte)t;
                        output[offset++] = (byte)(t >> 8);
                    }
                }

                dataSize -= 8 + chunk.CompressedSize;
            }

            return(output);
        }
Esempio n. 37
0
        private static bool LoadAppendMenu_old(HMENU hMenuParent, Stream stream)
        {
            // Read flags
            ushort flags = (ushort)stream.ReadByte();

            if ((flags & MF_POPUP) == 0)
            {
                // Regular menu item
                ushort id   = stream.ReadUInt16();
                string text = stream.ReadNullTerminatedString();
                if (text.StartsWith("\u0008"))
                {
                    flags |= MF_HELP;
                    text   = text.Substring(1);
                }
                if (string.IsNullOrEmpty(text))
                {
                    text = null;
                }
                User.AppendMenu(hMenuParent, (uint)(flags & ~(MF_END | MF_BITMAP)), (IntPtr)id, text);
            }
            else
            {
                // Popup menu item
                string text = stream.ReadNullTerminatedString();

                // Load all items
                var popup = User.CreatePopupMenu();
                while (LoadAppendMenu_old(popup, stream))
                {
                    ;
                }

                // Add it
                User.AppendMenu(hMenuParent, (uint)(flags & ~MF_END), popup.value, text);
            }

            return((flags & MF_END) == 0);
        }
Esempio n. 38
0
        public ValueSerializer GetDeserializerByManifest(Stream stream, DeserializerSession session)
        {
            //magic byte, a homage to Apache Kafka
            var magicByte = stream.ReadByte();

            if (magicByte <= 250)
            {
                return(_deserializerLookup[magicByte]);
            }
            switch (magicByte)
            {
            case ConsistentArraySerializer.Manifest:
                return(ConsistentArraySerializer.Instance);

            case ObjectReferenceSerializer.Manifest:
                return(ObjectReferenceSerializer.Instance);

            case ObjectSerializer.ManifestFull:
            {
                var type = TypeEx.GetTypeFromManifestFull(stream, session);
                return(GetCustomDeserializer(type));
            }

            case ObjectSerializer.ManifestIndex:
            {
                var typeId = (int)stream.ReadUInt16(session);
                if (typeId < _knownValueSerializers.Length)
                {
                    return(_knownValueSerializers[typeId]);
                }
                var type = TypeEx.GetTypeFromManifestIndex(typeId, session);
                return(GetCustomDeserializer(type));
            }

            default:
                throw new NotSupportedException("Unknown manifest value");
            }
        }
Esempio n. 39
0
        public static bool LoadSound(Stream s, out Func <Stream> result, out int sampleRate)
        {
            result = null;
            var startPosition = s.Position;

            try
            {
                sampleRate = s.ReadUInt16();
                var dataSize   = s.ReadInt32();
                var outputSize = s.ReadInt32();

                var readFlag = s.ReadByte();
                if (!Enum.IsDefined(typeof(SoundFlags), readFlag))
                {
                    return(false);
                }

                var readFormat = s.ReadByte();
                if (!Enum.IsDefined(typeof(SoundFormat), readFormat))
                {
                    return(false);
                }

                var offsetPosition = s.Position;

                result = () =>
                {
                    var audioStream = SegmentStream.CreateWithoutOwningStream(s, offsetPosition, (int)(s.Length - offsetPosition));
                    return(new AudStream(audioStream, outputSize, dataSize));
                };
            }
            finally
            {
                s.Position = startPosition;
            }

            return(true);
        }
Esempio n. 40
0
        static bool IsShpTD(Stream s)
        {
            var start = s.Position;

            // First word is the image count
            var imageCount = s.ReadUInt16();

            if (imageCount == 0)
            {
                s.Position = start;
                return(false);
            }

            // Last offset should point to the end of file
            var finalOffset = start + 14 + 8 * imageCount;

            if (finalOffset > s.Length)
            {
                s.Position = start;
                return(false);
            }

            s.Position = finalOffset;
            var eof = s.ReadUInt32();

            if (eof != s.Length)
            {
                s.Position = start;
                return(false);
            }

            // Check the format flag on the first frame
            s.Position = start + 17;
            var b = s.ReadUInt8();

            s.Position = start;
            return(b == 0x20 || b == 0x40 || b == 0x80);
        }
Esempio n. 41
0
        public Blit(Stream stream)
        {
            this.Frames = new BlitFrame[stream.ReadInt32()];
            var frameOffsets = new int[this.Frames.Length];

            stream.ReadInt32();             // TODO Unk
            var paletteOffset = stream.ReadInt32();
            var identifier    = new string(stream.ReadASCII(4).Reverse().ToArray());

            if (identifier != "BLT8")
            {
                throw new("Unknown blit type.");
            }

            for (var i = 0; i < this.Frames.Length; i++)
            {
                frameOffsets[i] = stream.ReadInt32();
            }

            stream.Position = paletteOffset;

            var palette = new byte[256 * 4];

            for (var i = 0; i < palette.Length;)
            {
                var color16 = stream.ReadUInt16();
                palette[i++] = (byte)(((color16 & 0x001f) << 3) & 0xff);
                palette[i++] = (byte)(((color16 & 0x03e0) >> 2) & 0xff);
                palette[i++] = (byte)(((color16 & 0x7c00) >> 7) & 0xff);
                palette[i++] = 0xff;
            }

            for (var i = 0; i < this.Frames.Length; i++)
            {
                stream.Position = frameOffsets[i];
                this.Frames[i]  = new(stream, palette);
            }
        }
Esempio n. 42
0
        private static Stream ReadLocalFile(Stream reader, out String fileName, out UInt32 decompressedSize, out UInt16 compressionMethod)
        {
            /*UInt16 version =*/
            reader.ReadUInt16();
            /*UInt16 bitflags =*/
            reader.ReadUInt16();
            compressionMethod = reader.ReadUInt16();

            if (compressionMethod != DeflateCompression && compressionMethod != StoreCompression)
            {
                throw new Exception("Invalid compression method " + compressionMethod);
            }

            /*UInt16 modtime =*/
            reader.ReadUInt16();
            /*UInt16 createtime =*/
            reader.ReadUInt16();
            /*UInt32 crc =*/
            reader.ReadUInt32();

            UInt32 compressedSize = reader.ReadUInt32();

            decompressedSize = reader.ReadUInt32();

            UInt16 nameLength  = reader.ReadUInt16();
            UInt16 fieldLength = reader.ReadUInt16();

            byte[] name = new byte[nameLength];
            reader.Read(name, 0, nameLength);
            /*byte[] fields =*/
            reader.Seek(fieldLength, SeekOrigin.Current);

            fileName = Encoding.UTF8.GetString(name);

            MemoryStream ms = new MemoryStream((int)compressedSize);

            CopyTo(reader, ms, compressedSize);
            ms.Position = 0;
            return(ms);
        }
Esempio n. 43
0
File: Vbc.cs Progetto: spooky/KKnD
        public Vbc(Stream stream)
        {
            if (stream.ReadASCII(4) != "SIFF")
            {
                throw new InvalidDataException("Invalid vbc (invalid SIFF section)");
            }

            stream.ReadUInt32();             // Length

            if (stream.ReadASCII(4) != "VBV1")
            {
                throw new InvalidDataException("Invalid vbc (not VBV1)");
            }

            if (stream.ReadASCII(4) != "VBHD")
            {
                throw new InvalidDataException("Invalid vbc (not VBHD)");
            }

            stream.ReadUInt32();             // Length
            stream.ReadUInt16();             // Version
            Size = new int2(stream.ReadUInt16(), stream.ReadUInt16());
            stream.ReadUInt32();             // 0
            Frames     = stream.ReadUInt16();
            SampleBits = stream.ReadUInt16();
            SampleRate = stream.ReadUInt16();
            stream.ReadUInt32();             // 0
            stream.ReadUInt32();             // 0
            stream.ReadUInt32();             // 0
            stream.ReadUInt32();             // 0

            if (stream.ReadASCII(4) != "BODY")
            {
                throw new InvalidDataException("Invalid vbc (not BODY)");
            }

            stream.ReadUInt32();             // Length

            frames = new VbcFrame[Frames];

            for (var i = 0; i < Frames; i++)
            {
                frames[i] = new VbcFrame(stream);
            }
        }
        public object Deserialize(GraphicsDevice device, Stream stream, string fileName)
        {
            var spriteSheet = new AnimatedSpriteSheet();

            var version = stream.ReadByte();

            spriteSheet.Name      = stream.ReadStringWithLength();
            spriteSheet.AssetName = stream.ReadStringWithLength();

            spriteSheet.StartDelay         = stream.ReadInt64();
            spriteSheet.TimeBetweenSprites = stream.ReadInt64();
            spriteSheet.Repeat             = stream.ReadBool();
            spriteSheet.ReverseAtEnd       = stream.ReadBool();

            ushort frameCount = stream.ReadUInt16();

            for (int i = 0; i < frameCount; i++)
            {
                Sprite sprite = new Sprite();

                stream.ReadUInt16(); // Frame index, unused

                sprite.Name   = stream.ReadStringWithLength();
                sprite.Width  = stream.ReadUInt16();
                sprite.Height = stream.ReadUInt16();
                sprite.X      = stream.ReadUInt16();
                sprite.Y      = stream.ReadUInt16();

                spriteSheet.Sprites[sprite.Name] = sprite;
            }

            using (var fs = new FileStream(Path.Combine(Path.GetDirectoryName(fileName), spriteSheet.AssetName + ".png"), FileMode.Open))
            {
                spriteSheet.Texture = Texture2D.FromStream(device, fs);
            }

            foreach (var sprite in spriteSheet.Sprites)
            {
                sprite.Value.Texture = spriteSheet.Texture;
            }


            return(spriteSheet);
        }
Esempio n. 45
0
        public void Read(Stream stream)
        {
            var magic = new byte[MAGIC.Length];

            stream.Read(magic);
            if (!MAGIC.SequenceEqual(magic))
            {
                throw new BadImageFormatException("Magic value is not present for an ELF file");
            }

            EI_CLASS      = stream.ReadByteAndParse <HeaderIdentityClass>(HeaderIdentityClass.ELFCLASSNONE);
            EI_DATA       = stream.ReadByteAndParse <HeaderIdentityData>(HeaderIdentityData.ELFDATANONE);
            EI_VERSION    = stream.ReadByteAndParse <HeaderIdentityVersion>(HeaderIdentityVersion.EI_CURRENT);
            EI_OSABI      = stream.ReadByteAndParse <HeaderOsAbiVersion>(HeaderOsAbiVersion.ELFOSABI_NONE);
            EI_ABIVERSION = (byte)stream.ReadByte();

            stream.Seek(16, SeekOrigin.Begin);
            E_TYPE      = stream.ReadHalfWord <HeaderType>(HeaderType.ET_NONE);
            E_MACHINE   = stream.ReadHalfWord <HeaderMachine>(HeaderMachine.EM_NONE);
            E_VERSION   = stream.ReadWord <HeaderVersion>(HeaderVersion.EV_NONE);
            E_ENTRY     = stream.ReadAddress32();
            E_PHOFF     = stream.ReadOffset32();
            E_SHOFF     = stream.ReadOffset32();
            E_FLAGS     = stream.ReadUInt32();
            E_EHSIZE    = stream.ReadUInt16();
            E_PHENTSIZE = stream.ReadUInt16();
            E_PHNUM     = stream.ReadUInt16();
            E_SHENTSIZE = stream.ReadUInt16();
            E_SHNUM     = stream.ReadUInt16();
            E_SHSTRNDX  = stream.ReadUInt16();

            if (E_EHSIZE != stream.Position && E_EHSIZE != 64)
            {
                throw new InvalidOperationException("E_EHSIZE does not equal the current reader position");
            }
        }
Esempio n. 46
0
        /// <summary>
        /// Reads an object from a stream.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <returns>Decoded object.</returns>
        public static object ReadObject(this Stream stream)
        {
            ObjectType type = (ObjectType)stream.ReadNextByte();

            switch (type)
            {
            case ObjectType.Null:
                return(null);

            case ObjectType.DBNull:
                return(DBNull.Value);

            case ObjectType.BooleanTrue:
                return(true);

            case ObjectType.BooleanFalse:
                return(false);

            case ObjectType.Char:
                return(stream.ReadChar());

            case ObjectType.SByte:
                return(stream.ReadSByte());

            case ObjectType.Byte:
                return(stream.ReadNextByte());

            case ObjectType.Int16:
                return(stream.ReadInt16());

            case ObjectType.UInt16:
                return(stream.ReadUInt16());

            case ObjectType.Int32:
                return(stream.ReadInt32());

            case ObjectType.UInt32:
                return(stream.ReadUInt32());

            case ObjectType.Int64:
                return(stream.ReadInt64());

            case ObjectType.UInt64:
                return(stream.ReadUInt64());

            case ObjectType.Single:
                return(stream.ReadSingle());

            case ObjectType.Double:
                return(stream.ReadDouble());

            case ObjectType.Decimal:
                return(stream.ReadDecimal());

            case ObjectType.DateTime:
                return(stream.ReadDateTime());

            case ObjectType.String:
                return(stream.ReadString());

            case ObjectType.ByteArray:
                return(stream.ReadBytes());

            case ObjectType.CharArray:
                return(stream.ReadString().ToCharArray());

            case ObjectType.Guid:
                return(stream.ReadGuid());

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 47
0
 private static uint ReadUInt32(this Stream stream)
 {
     return(unchecked ((uint)((uint)stream.ReadUInt16() | ((uint)stream.ReadUInt16() << 16))));
 }
Esempio n. 48
0
 private static short ReadInt16(this Stream stream)
 {
     return(unchecked ((short)stream.ReadUInt16()));
 }
Esempio n. 49
0
        public static byte[] LoadSound(Stream s)
        {
            /*var sampleRate =*/ s.ReadUInt16();
            var dataSize = s.ReadInt32();
            var outputSize = s.ReadInt32();
            /*var flags = (SoundFlags)*/ s.ReadByte();
            /*var format = (SoundFormat)*/ s.ReadByte();

            var output = new byte[outputSize];
            var offset = 0;
            var index = 0;
            var currentSample = 0;

            while (dataSize > 0)
            {
                var chunk = Chunk.Read(s);
                for (int n = 0; n < chunk.CompressedSize; n++)
                {
                    var b = s.ReadUInt8();

                    var t = DecodeSample(b, ref index, ref currentSample);
                    output[offset++] = (byte)t;
                    output[offset++] = (byte)(t >> 8);

                    if (offset < outputSize)
                    {
                        /* possible that only half of the final byte is used! */
                        t = DecodeSample((byte)(b >> 4), ref index, ref currentSample);
                        output[offset++] = (byte)t;
                        output[offset++] = (byte)(t >> 8);
                    }
                }

                dataSize -= 8 + chunk.CompressedSize;
            }

            return output;
        }
Esempio n. 50
0
        public byte WindowsSDKVersion;            // 3F Windows SDK version number

        public static IMAGE_OS2_HEADER Deserialize(Stream stream)
        {
            var ioh = new IMAGE_OS2_HEADER();

            ioh.Magic                       = stream.ReadUInt16();
            ioh.LinkerVersion               = stream.ReadByteValue();
            ioh.LinkerRevision              = stream.ReadByteValue();
            ioh.EntryTableOffset            = stream.ReadUInt16();
            ioh.EntryTableSize              = stream.ReadUInt16();
            ioh.CrcChecksum                 = stream.ReadUInt32();
            ioh.Flags                       = stream.ReadUInt16();
            ioh.Autodata                    = stream.ReadUInt16();
            ioh.InitialHeapAlloc            = stream.ReadUInt16();
            ioh.InitialStackAlloc           = stream.ReadUInt16();
            ioh.InitialCSIPSetting          = stream.ReadUInt32();
            ioh.InitialSSSPSetting          = stream.ReadUInt32();
            ioh.FileSegmentCount            = stream.ReadUInt16();
            ioh.ModuleReferenceTableSize    = stream.ReadUInt16();
            ioh.NonResidentNameTableSize    = stream.ReadUInt16();
            ioh.SegmentTableOffset          = stream.ReadUInt16();
            ioh.ResourceTableOffset         = stream.ReadUInt16();
            ioh.ResidentNameTableOffset     = stream.ReadUInt16();
            ioh.ModuleReferenceTableOffset  = stream.ReadUInt16();
            ioh.ImportedNamesTableOffset    = stream.ReadUInt16();
            ioh.NonResidentNamesTableOffset = stream.ReadUInt32();
            ioh.MovableEntriesCount         = stream.ReadUInt16();
            ioh.SegmentAlignmentShiftCount  = stream.ReadUInt16();
            ioh.ResourceEntriesCount        = stream.ReadUInt16();
            ioh.TargetOperatingSystem       = stream.ReadByteValue();
            ioh.AdditionalFlags             = stream.ReadByteValue();
            ioh.Reserved                    = new ushort[Constants.NERESWORDS];
            for (int i = 0; i < Constants.NERESWORDS; i++)
            {
                ioh.Reserved[i] = stream.ReadUInt16();
            }
            ioh.WindowsSDKRevision = stream.ReadByteValue();
            ioh.WindowsSDKVersion  = stream.ReadByteValue();

            return(ioh);
        }
Esempio n. 51
0
            public FileDescriptor(Stream stream, uint index, long tableOffset)
            {
                Index = index;
                Flags = (CABFlags)stream.ReadUInt16();
                ExpandedSize = stream.ReadUInt32();
                stream.Position += 4;
                CompressedSize = stream.ReadUInt32();

                stream.Position += 4;
                DataOffset = stream.ReadUInt32();
                stream.Position += 4;
                MD5 = stream.ReadBytes(16);

                stream.Position += 16;
                NameOffset = stream.ReadUInt32();
                DirectoryIndex = stream.ReadUInt16();
                stream.Position += 12;
                LinkToPrevious = stream.ReadUInt32();
                LinkToNext = stream.ReadUInt32();

                LinkFlags = (LinkFlags)stream.ReadUInt8();
                Volume = stream.ReadUInt16();

                var pos = stream.Position;
                stream.Position = tableOffset + NameOffset;
                Filename = stream.ReadASCIIZ();
                stream.Position = pos;
            }
			public FileDescriptor(Stream reader, long tableOffset)
			{
				Flags = reader.ReadUInt16();
				ExpandedSize = reader.ReadUInt32();
				/*    unknown   */ reader.ReadUInt32();
				CompressedSize = reader.ReadUInt32();

				/*  unknown */ reader.ReadUInt32();
				DataOffset   = reader.ReadUInt32();
				/*  unknown */ reader.ReadUInt32();
				MD5          = reader.ReadBytes(16);

				/*   unknown  */ reader.ReadBytes(16);
				NameOffset     = reader.ReadUInt32();
				DirectoryIndex = reader.ReadUInt16();
				/*   unknown  */ reader.ReadBytes(12);
				LinkToPrevious = reader.ReadUInt32();
				LinkToNext = reader.ReadUInt32();

				LinkFlags = reader.ReadBytes(1)[0];
				Volume = reader.ReadUInt16();
				var posSave = reader.Position;

				reader.Seek(tableOffset + NameOffset, SeekOrigin.Begin);
				Filename = reader.ReadASCIIZ();
				reader.Seek(posSave, SeekOrigin.Begin);
			}
Esempio n. 53
0
            public ShpTSFrame(Stream s, Size frameSize)
            {
                var x      = s.ReadUInt16();
                var y      = s.ReadUInt16();
                var width  = s.ReadUInt16();
                var height = s.ReadUInt16();

                // Pad the dimensions to an even number to avoid issues with half-integer offsets
                var dataWidth  = width;
                var dataHeight = height;

                if (dataWidth % 2 == 1)
                {
                    dataWidth += 1;
                }

                if (dataHeight % 2 == 1)
                {
                    dataHeight += 1;
                }

                Offset    = new int2(x + (dataWidth - frameSize.Width) / 2, y + (dataHeight - frameSize.Height) / 2);
                Size      = new Size(dataWidth, dataHeight);
                FrameSize = frameSize;

                Format      = s.ReadUInt8();
                s.Position += 11;
                FileOffset  = s.ReadUInt32();

                if (FileOffset == 0)
                {
                    return;
                }

                // Parse the frame data as we go (but remember to jump back to the header before returning!)
                var start = s.Position;

                s.Position = FileOffset;

                Data = new byte[dataWidth * dataHeight];

                if (Format == 3)
                {
                    // Format 3 provides RLE-zero compressed scanlines
                    for (var j = 0; j < height; j++)
                    {
                        var length = s.ReadUInt16() - 2;
                        RLEZerosCompression.DecodeInto(s.ReadBytes(length), Data, dataWidth * j);
                    }
                }
                else
                {
                    // Format 2 provides uncompressed length-prefixed scanlines
                    // Formats 1 and 0 provide an uncompressed full-width row
                    var length = Format == 2 ? s.ReadUInt16() - 2 : width;
                    for (var j = 0; j < height; j++)
                    {
                        s.ReadBytes(Data, dataWidth * j, length);
                    }
                }

                s.Position = start;
            }
Esempio n. 54
0
        public VqaReader(Stream stream)
        {
            this.stream = stream;

            // Decode FORM chunk
            if (stream.ReadASCII(4) != "FORM")
            {
                throw new InvalidDataException("Invalid vqa (invalid FORM section)");
            }
            /*var length = */ stream.ReadUInt32();

            if (stream.ReadASCII(8) != "WVQAVQHD")
            {
                throw new InvalidDataException("Invalid vqa (not WVQAVQHD)");
            }
            /*var length2 = */ stream.ReadUInt32();

            /*var version = */ stream.ReadUInt16();
            videoFlags = stream.ReadUInt16();
            Frames     = stream.ReadUInt16();
            Width      = stream.ReadUInt16();
            Height     = stream.ReadUInt16();

            blockWidth       = stream.ReadUInt8();
            blockHeight      = stream.ReadUInt8();
            Framerate        = stream.ReadUInt8();
            chunkBufferParts = stream.ReadUInt8();
            blocks           = new int2(Width / blockWidth, Height / blockHeight);

            numColors = stream.ReadUInt16();
            /*var maxBlocks = */ stream.ReadUInt16();
            /*var unknown1 = */ stream.ReadUInt16();
            /*var unknown2 = */ stream.ReadUInt32();

            // Audio
            sampleRate    = stream.ReadUInt16();
            audioChannels = stream.ReadByte();
            sampleBits    = stream.ReadByte();

            /*var unknown3 =*/ stream.ReadUInt32();
            /*var unknown4 =*/ stream.ReadUInt16();
            /*maxCbfzSize =*/ stream.ReadUInt32();            // Unreliable

            /*var unknown5 =*/ stream.ReadUInt32();

            var frameSize = Exts.NextPowerOf2(Math.Max(Width, Height));

            if (IsHqVqa)
            {
                cbfBuffer = new byte[maxCbfzSize];
                cbf       = new byte[maxCbfzSize * 3];
                origData  = new byte[maxCbfzSize];
            }
            else
            {
                cbfBuffer = new byte[Width * Height];
                cbf       = new byte[Width * Height];
                cbp       = new byte[Width * Height];
                origData  = new byte[2 * blocks.X * blocks.Y];
            }

            palette   = new uint[numColors];
            frameData = new uint[frameSize, frameSize];
            var type = stream.ReadASCII(4);

            while (type != "FINF")
            {
                // Sub type is a file tag
                if (type[3] == 'F')
                {
                    var jmp = int2.Swap(stream.ReadUInt32());
                    stream.Seek(jmp, SeekOrigin.Current);
                    type = stream.ReadASCII(4);
                }
                else
                {
                    throw new NotSupportedException("Vqa uses unknown Subtype: {0}".F(type));
                }
            }

            /*var length = */ stream.ReadUInt16();
            /*var unknown4 = */ stream.ReadUInt16();

            // Frame offsets
            offsets = new uint[Frames];
            for (var i = 0; i < Frames; i++)
            {
                offsets[i] = stream.ReadUInt32();
                if (offsets[i] > 0x40000000)
                {
                    offsets[i] -= 0x40000000;
                }
                offsets[i] <<= 1;
            }

            CollectAudioData();

            Reset();
        }
Esempio n. 55
0
        static void Preload(Stream stream, out VocBlock[] blocks, out int totalSamples, out int sampleRate)
        {
            var blockList = new List <VocBlock>();

            totalSamples = 0;
            sampleRate   = 0;

            while (true)
            {
                var block = default(VocBlock);
                try
                {
                    block.Code   = stream.ReadByte();
                    block.Length = 0;
                }
                catch (EndOfStreamException)
                {
                    // Stream is allowed to end without a last block
                    break;
                }

                if (block.Code == 0 || block.Code > 9)
                {
                    break;
                }

                block.Length  = stream.ReadByte();
                block.Length |= stream.ReadByte() << 8;
                block.Length |= stream.ReadByte() << 16;

                var skip = 0;
                switch (block.Code)
                {
                // Sound data
                case 1:
                {
                    if (block.Length < 2)
                    {
                        throw new InvalidDataException("Invalid sound data block length in voc file");
                    }
                    var freqDiv = stream.ReadByte();
                    block.SampleBlock.Rate = GetSampleRateFromVocRate(freqDiv);
                    var codec = stream.ReadByte();
                    if (codec != 0)
                    {
                        throw new InvalidDataException("Unhandled codec used in voc file");
                    }
                    skip = block.Length - 2;
                    block.SampleBlock.Samples = skip;
                    block.SampleBlock.Offset  = stream.Position;

                    // See if last block contained additional information
                    if (blockList.Count > 0)
                    {
                        var b = blockList.Last();
                        if (b.Code == 8)
                        {
                            block.SampleBlock.Rate = b.SampleBlock.Rate;
                            blockList.Remove(b);
                        }
                    }

                    sampleRate = Math.Max(sampleRate, block.SampleBlock.Rate);
                    break;
                }

                // Silence
                case 3:
                {
                    if (block.Length != 3)
                    {
                        throw new InvalidDataException("Invalid silence block length in voc file");
                    }
                    block.SampleBlock.Offset  = 0;
                    block.SampleBlock.Samples = stream.ReadUInt16() + 1;
                    var freqDiv = stream.ReadByte();
                    block.SampleBlock.Rate = GetSampleRateFromVocRate(freqDiv);
                    break;
                }

                // Repeat start
                case 6:
                {
                    if (block.Length != 2)
                    {
                        throw new InvalidDataException("Invalid repeat start block length in voc file");
                    }
                    block.LoopBlock.Count = stream.ReadUInt16() + 1;
                    break;
                }

                // Repeat end
                case 7:
                    break;

                // Extra info
                case 8:
                {
                    if (block.Length != 4)
                    {
                        throw new InvalidDataException("Invalid info block length in voc file");
                    }
                    int freqDiv = stream.ReadUInt16();
                    if (freqDiv == 65536)
                    {
                        throw new InvalidDataException("Invalid frequency divisor 65536 in voc file");
                    }
                    var codec = stream.ReadByte();
                    if (codec != 0)
                    {
                        throw new InvalidDataException("Unhandled codec used in voc file");
                    }
                    var channels = stream.ReadByte() + 1;
                    if (channels != 1)
                    {
                        throw new InvalidDataException("Unhandled number of channels in voc file");
                    }
                    block.SampleBlock.Offset  = 0;
                    block.SampleBlock.Samples = 0;
                    block.SampleBlock.Rate    = (int)(256000000L / (65536L - freqDiv));
                    break;
                }

                // Sound data (New format)
                case 9:
                default:
                    throw new InvalidDataException("Unhandled code in voc file");
                }

                if (skip > 0)
                {
                    stream.Seek(skip, SeekOrigin.Current);
                }
                blockList.Add(block);
            }

            // Check validity and calculated total number of samples
            foreach (var b in blockList)
            {
                if (b.Code == 8)
                {
                    throw new InvalidDataException("Unused block 8 in voc file");
                }
                if (b.Code != 1 && b.Code != 9)
                {
                    continue;
                }
                if (b.SampleBlock.Rate != sampleRate)
                {
                    throw new InvalidDataException("Voc file contains chunks with different sample rate");
                }
                totalSamples += b.SampleBlock.Samples;
            }

            blocks = blockList.ToArray();
        }
Esempio n. 56
0
        public static float SoundLength(Stream s)
        {
            var sampleRate = s.ReadUInt16();
            /*var dataSize = */ s.ReadInt32();
            var outputSize = s.ReadInt32();
            var flags = (SoundFlags)s.ReadByte();

            var samples = outputSize;
            if (0 != (flags & SoundFlags.Stereo)) samples /= 2;
            if (0 != (flags & SoundFlags._16Bit)) samples /= 2;
            return samples / sampleRate;
        }
Esempio n. 57
0
        public VqaReader(Stream stream)
        {
            this.stream = stream;

            // Decode FORM chunk
            if (stream.ReadASCII(4) != "FORM")
                throw new InvalidDataException("Invalid vqa (invalid FORM section)");
            /*var length = */ stream.ReadUInt32();

            if (stream.ReadASCII(8) != "WVQAVQHD")
                throw new InvalidDataException("Invalid vqa (not WVQAVQHD)");
            /* var length = */stream.ReadUInt32();

            /*var version = */stream.ReadUInt16();
            /*var flags = */stream.ReadUInt16();
            Frames = stream.ReadUInt16();
            Width = stream.ReadUInt16();
            Height = stream.ReadUInt16();

            blockWidth = stream.ReadUInt8();
            blockHeight = stream.ReadUInt8();
            Framerate = stream.ReadUInt8();
            cbParts = stream.ReadUInt8();
            blocks = new int2(Width / blockWidth, Height / blockHeight);

            numColors = stream.ReadUInt16();
            /*var maxBlocks = */stream.ReadUInt16();
            /*var unknown1 = */stream.ReadUInt16();
            /*var unknown2 = */stream.ReadUInt32();

            // Audio
            /*var freq = */stream.ReadUInt16();
            /*var channels = */stream.ReadByte();
            /*var bits = */stream.ReadByte();
            /*var unknown3 = */stream.ReadBytes(14);

            var frameSize = Exts.NextPowerOf2(Math.Max(Width, Height));
            cbf = new byte[Width*Height];
            cbp = new byte[Width*Height];
            palette = new uint[numColors];
            origData = new byte[2*blocks.X*blocks.Y];
            frameData = new uint[frameSize, frameSize];

            var type = stream.ReadASCII(4);
            if (type != "FINF")
            {
                stream.Seek(27, SeekOrigin.Current);
                type = stream.ReadASCII(4);
            }

            /*var length = */stream.ReadUInt16();
            /*var unknown4 = */stream.ReadUInt16();

            // Frame offsets
            offsets = new UInt32[Frames];
            for (var i = 0; i < Frames; i++)
            {
                offsets[i] = stream.ReadUInt32();
                if (offsets[i] > 0x40000000)
                    offsets[i] -= 0x40000000;
                offsets[i] <<= 1;
            }

            CollectAudioData();

            Reset();
        }
Esempio n. 58
0
		ShpD2Frame[] ParseFrames(Stream s)
		{
			var start = s.Position;

			var imageCount = s.ReadUInt16();

			// Last offset is pointer to end of file.
			var offsets = new uint[imageCount + 1];
			var temp = s.ReadUInt32();

			// If fourth byte in file is non-zero, the offsets are two bytes each.
			var twoByteOffset = (temp & 0xFF0000) > 0;
			s.Position = 2;

			for (var i = 0; i < imageCount + 1; i++)
				offsets[i] = (twoByteOffset ? s.ReadUInt16() : s.ReadUInt32()) + 2;

			var frames = new ShpD2Frame[imageCount];
			for (var i = 0; i < frames.Length; i++)
			{
				s.Position = offsets[i];
				frames[i] = new ShpD2Frame(s);
			}

			s.Position = start;
			return frames;
		}
Esempio n. 59
0
        private void loadHeader(Stream stream)
        {
            uint tag = stream.ReadUInt32();

            if (tag != SfarTag)
            {
                throw new Exception("Wrong SFAR tag");
            }
            uint sfarVersion = stream.ReadUInt32();

            if (sfarVersion != SfarVersion)
            {
                throw new Exception("Wrong SFAR version");
            }

            uint dataOffset    = stream.ReadUInt32();
            uint entriesOffset = stream.ReadUInt32();

            filesCount = stream.ReadUInt32();
            uint sizesArrayOffset = stream.ReadUInt32();

            maxBlockSize = stream.ReadUInt32();
            uint compressionTag = stream.ReadUInt32();

            if (compressionTag != LZMATag)
            {
                throw new Exception("Not LZMA compression for SFAR file");
            }

            uint numBlockSizes = 0;

            stream.JumpTo(entriesOffset);
            filesList = new List <FileEntry>();
            for (int i = 0; i < filesCount; i++)
            {
                FileEntry file = new FileEntry();
                file.filenameHash = stream.ReadToBuffer(16);
                file.compressedBlockSizesIndex = stream.ReadInt32();
                file.uncomprSize  = stream.ReadUInt32();
                file.uncomprSize |= (long)stream.ReadByte() << 32;
                file.dataOffset   = stream.ReadUInt32();
                file.dataOffset  |= (long)stream.ReadByte() << 32;
                file.numBlocks    = (uint)((file.uncomprSize + maxBlockSize - 1) / maxBlockSize);
                filesList.Add(file);
                numBlockSizes += file.numBlocks;
            }

            stream.JumpTo(sizesArrayOffset);
            blockSizes = new List <ushort>();
            for (int i = 0; i < numBlockSizes; i++)
            {
                blockSizes.Add(stream.ReadUInt16());
            }

            filenamesIndex = -1;
            for (int i = 0; i < filesCount; i++)
            {
                if (StructuralComparisons.StructuralEqualityComparer.Equals(filesList[i].filenameHash, FileListHash))
                {
                    stream.JumpTo(filesList[i].dataOffset);
                    int    compressedBlockSize = blockSizes[filesList[i].compressedBlockSizesIndex];
                    byte[] inBuf  = stream.ReadToBuffer(compressedBlockSize);
                    byte[] outBuf = SevenZipHelper.LZMA.Decompress(inBuf, (uint)filesList[i].uncomprSize);
                    if (outBuf.Length == 0)
                    {
                        throw new Exception();
                    }
                    StreamReader filenamesStream = new StreamReader(new MemoryStream(outBuf));
                    while (filenamesStream.EndOfStream == false)
                    {
                        string name = filenamesStream.ReadLine();
                        byte[] hash = MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(name.ToLowerInvariant()));
                        for (int l = 0; l < filesCount; l++)
                        {
                            if (StructuralComparisons.StructuralEqualityComparer.Equals(filesList[l].filenameHash, hash))
                            {
                                FileEntry f = filesList[l];
                                f.filenamePath = name;
                                filesList[l]   = f;
                            }
                        }
                    }
                    filenamesIndex = i;
                    break;
                }
            }
            if (filenamesIndex == -1)
            {
                throw new Exception("filenames entry not found");
            }
        }
Esempio n. 60
0
            public ShpTSFrame(Stream s, Size frameSize)
            {
                var x = s.ReadUInt16();
                var y = s.ReadUInt16();
                var width = s.ReadUInt16();
                var height = s.ReadUInt16();

                // Pad the dimensions to an even number to avoid issues with half-integer offsets
                var dataWidth = width;
                var dataHeight = height;
                if (dataWidth % 2 == 1)
                    dataWidth += 1;

                if (dataHeight % 2 == 1)
                    dataHeight += 1;

                Offset = new int2(x + (dataWidth - frameSize.Width) / 2, y + (dataHeight - frameSize.Height) / 2);
                Size = new Size(dataWidth, dataHeight);
                FrameSize = frameSize;

                Format = s.ReadUInt8();
                s.Position += 11;
                FileOffset = s.ReadUInt32();

                if (FileOffset == 0)
                    return;

                // Parse the frame data as we go (but remember to jump back to the header before returning!)
                var start = s.Position;
                s.Position = FileOffset;

                Data = new byte[dataWidth * dataHeight];

                if (Format == 3)
                {
                    // Format 3 provides RLE-zero compressed scanlines
                    for (var j = 0; j < height; j++)
                    {
                        var length = s.ReadUInt16() - 2;
                        Format2.DecodeInto(s.ReadBytes(length), Data, dataWidth * j);
                    }
                }
                else
                {
                    // Format 2 provides uncompressed length-prefixed scanlines
                    // Formats 1 and 0 provide an uncompressed full-width row
                    var length = Format == 2 ? s.ReadUInt16() - 2 : width;
                    for (var j = 0; j < height; j++)
                        s.ReadBytes(Data, dataWidth * j, length);
                }

                s.Position = start;
            }