示例#1
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;
        }
示例#2
0
 public static void ReadBlock(Stream stream, out BlockType blockType, out byte[] data)
 {
     blockType = (BlockType)stream.ReadByte();
     var sizeBuffer = stream.ReadBytes(2);
     var size = (ushort)(sizeBuffer[0] | sizeBuffer[1] << 8);
     data = stream.ReadBytes(size - 2);
 }
示例#3
0
        protected override void LoadFromStream(Stream stream)
        {
            base.LoadFromStream(stream);

            if (Version == 1)
            {
                _CreationTime = stream.ReadBEUInt64();
                _ModificationTime = stream.ReadBEUInt64();
                TimeScale = stream.ReadBEUInt32();
                Duration = stream.ReadBEUInt64();
            }
            else // if(Version == 0)
            {
                _CreationTime = stream.ReadBEUInt32();
                _ModificationTime = stream.ReadBEUInt32();
                TimeScale = stream.ReadBEUInt32();
                Duration = stream.ReadBEUInt32();
            }
            _Rate = stream.ReadBEInt32();
            _Volume = stream.ReadBEInt16();
            Reserved = stream.ReadBytes(2 + (2 * 4));
            for (int i = 0; i < 9; i++) Matrix[i] = stream.ReadBEInt32();
            PreDefined = stream.ReadBytes(6 * 4);
            NextTrackID = stream.ReadBEUInt32();
        }
示例#4
0
		public WavLoader(Stream s)
		{
			while (s.Position < s.Length)
			{
				if ((s.Position & 1) == 1)
					s.ReadByte(); // Alignment

				var type = s.ReadASCII(4);
				switch (type)
				{
					case "RIFF":
						FileSize = s.ReadInt32();
						Format = s.ReadASCII(4);
						if (Format != "WAVE")
							throw new NotSupportedException("Not a canonical WAVE file.");
						break;
					case "fmt ":
						FmtChunkSize = s.ReadInt32();
						AudioFormat = s.ReadInt16();
						Type = (WaveType)AudioFormat;
						if (Type != WaveType.Pcm && Type != WaveType.ImaAdpcm)
							throw new NotSupportedException("Compression type is not supported.");
						Channels = s.ReadInt16();
						SampleRate = s.ReadInt32();
						ByteRate = s.ReadInt32();
						BlockAlign = s.ReadInt16();
						BitsPerSample = s.ReadInt16();

						s.ReadBytes(FmtChunkSize - 16);
						break;
					case "fact":
						{
							var chunkSize = s.ReadInt32();
							UncompressedSize = s.ReadInt32();
							s.ReadBytes(chunkSize - 4);
						}

						break;
					case "data":
						DataSize = s.ReadInt32();
						RawOutput = s.ReadBytes(DataSize);
						break;
					default:
						// Ignore unknown chunks
						{
							var chunkSize = s.ReadInt32();
							s.ReadBytes(chunkSize);
						}

						break;
				}
			}

			if (Type == WaveType.ImaAdpcm)
			{
				RawOutput = DecodeImaAdpcmData();
				BitsPerSample = 16;
			}
		}
示例#5
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>());
        }
示例#6
0
        public static Account Decrypt(Stream inputStream, ConsoleType consoleType)
        {
            var hmac = new HMACSHA1(consoleType == ConsoleType.Retail ? RetailKey : DevkitKey);
            var hash = inputStream.ReadBytes(16);
            var rc4Key = hmac.ComputeHash(hash);
            Array.Resize(ref rc4Key, 16);

            var rest = inputStream.ReadBytes(388);
            var body = RC4.Decrypt(rc4Key, rest);

            var compareBuffer = hmac.ComputeHash(body);
            if (!memcmp(hash, compareBuffer, 16))
                throw new InvalidDataException("Keys do not match");
            return ModelFactory.GetModel<Account>(body.Skip(8).ToArray());
        }
示例#7
0
			public R8Frame(Stream s)
			{
				// Scan forward until we find some data
				var type = s.ReadUInt8();
				while (type == 0)
					type = s.ReadUInt8();

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

				Size = new Size(width, height);
				Offset = new int2(width / 2 - x, height / 2 - y);

				/*var imageOffset = */
				s.ReadInt32();
				var paletteOffset = s.ReadInt32();
				var bpp = s.ReadUInt8();
				if (bpp != 8)
					throw new InvalidDataException("Error: {0} bits per pixel are not supported.".F(bpp));

				var frameHeight = s.ReadUInt8();
				var frameWidth = s.ReadUInt8();
				FrameSize = new Size(frameWidth, frameHeight);

				// Skip alignment byte
				s.ReadUInt8();

				Data = s.ReadBytes(width * height);

				// Ignore palette
				if (type == 1 && paletteOffset != 0)
					s.Seek(520, SeekOrigin.Current);
			}
示例#8
0
        public DatabaseObject Decode(Stream source)
        {
            if (source == null) { throw new ArgumentNullException("source"); }

            // Create a binary reader that can be disposed without disposing the underlying stream.
            string type;
            string lenStr;
            using (BinaryReader reader = new BinaryReader(new DisposeProtectedStream(source)))
            {
                // First token is the type
                type = ReadToken(reader);

                // Second is the len
                lenStr = ReadToken(reader);
            }

            long len = -1;
            if(!Int64.TryParse(lenStr, out len)) {
                throw new InvalidDataException(String.Format("Invalid object, length token value is not an integer: {0}", lenStr));
            }

            // Should now be at the object body, read that in (for now)
            byte[] data = source.ReadBytes(len);
            return new DatabaseObject(DatabaseObjectTypeHelper.Parse(type), data);
        }
示例#9
0
 public static String Decode(Stream stream)
 {
     int len = Int32EncodingLE.Decode(stream);
     if (len == -1) return null;
     byte[] buf = stream.ReadBytes(len);
     return Decode(buf);
 }
示例#10
0
 public Stream Open(Stream stream)
 {
     stream.Seek(DataOffset, SeekOrigin.Begin);
     byte[] data;
     if (DataCompression == 0)
     {
         data = stream.ReadBytes(DataSize);
     }
     else
     {
         var dataBuffer = stream.ReadBytes(DataCompressedSize);
         var inflater = new Inflater(false);
         inflater.SetInput(dataBuffer);
         data = new Byte[DataSize];
         inflater.Inflate(data);
     }
     return new MemoryStream(data);
 }
示例#11
0
        protected override void LoadFromStream(Stream stream)
        {
            base.LoadFromStream(stream);

            _Predefined = stream.ReadBEUInt32();
            HandlerType = new FourCC(stream.ReadBytes(4));
            for (int i = 0; i < _Reserved.Length; i++) _Reserved[i] = stream.ReadBEUInt32();
            Name = stream.ReadNullTerminatedUTF8String();
        }
			public FileGroup(Stream reader, long offset)
			{
				var nameOffset = reader.ReadUInt32();
				/*   unknown  */ reader.ReadBytes(18);
				FirstFile = reader.ReadUInt32();
				LastFile = reader.ReadUInt32();

				reader.Seek(offset + (long)nameOffset, SeekOrigin.Begin);
				Name = reader.ReadASCIIZ();
			}
示例#13
0
        public InstallShieldCABExtractor(FileSystem context, string hdrFilename)
        {
            var fileGroups = new List<FileGroup>();
            var fileGroupOffsets = new List<uint>();

            hdrFile = context.Open(hdrFilename);
            this.context = context;

            // Strips archive number AND file extension
            Name = Regex.Replace(hdrFilename, @"\d*\.[^\.]*$", "");
            var signature = hdrFile.ReadUInt32();

            if (signature != 0x28635349)
                throw new InvalidDataException("Not an Installshield CAB package");

            commonHeader = new CommonHeader(hdrFile);
            cabDescriptor = new CabDescriptor(hdrFile, commonHeader);
            /*    unknown   */ hdrFile.ReadBytes(14);

            for (var i = 0U; i < MaxFileGroupCount; ++i)
                fileGroupOffsets.Add(hdrFile.ReadUInt32());

            hdrFile.Seek(commonHeader.CabDescriptorOffset + cabDescriptor.FileTableOffset, SeekOrigin.Begin);
            directoryTable = new List<uint>();

            for (var i = 0U; i < cabDescriptor.DirectoryCount; ++i)
                directoryTable.Add(hdrFile.ReadUInt32());

            foreach (var offset in fileGroupOffsets)
            {
                var nextOffset = offset;
                while (nextOffset != 0)
                {
                    hdrFile.Seek((long)nextOffset + 4 + commonHeader.CabDescriptorOffset, SeekOrigin.Begin);
                    var descriptorOffset = hdrFile.ReadUInt32();
                    nextOffset = hdrFile.ReadUInt32();
                    hdrFile.Seek(descriptorOffset + commonHeader.CabDescriptorOffset, SeekOrigin.Begin);

                    fileGroups.Add(new FileGroup(hdrFile, commonHeader.CabDescriptorOffset));
                }
            }

            hdrFile.Seek(commonHeader.CabDescriptorOffset + cabDescriptor.FileTableOffset + cabDescriptor.FileTableOffset2, SeekOrigin.Begin);
            foreach (var fileGroup in fileGroups)
            {
                for (var i = fileGroup.FirstFile; i <= fileGroup.LastFile; ++i)
                {
                    AddFileDescriptorToList(i);
                    var fileDescriptor = fileDescriptors[i];
                    var fullFilePath   = "{0}\\{1}\\{2}".F(fileGroup.Name, DirectoryName(fileDescriptor.DirectoryIndex), fileDescriptor.Filename);
                    index.Add(fullFilePath, i);
                }
            }
        }
		public static ApplicationData Read(Stream stream)
		{
			var blockSize = stream.ReadByte();
			if (blockSize <= 0)
			{
				return null;
			}

			var data = stream.ReadBytes(blockSize);
			return new ApplicationData((byte) blockSize, data);
		}
示例#15
0
        public WavLoader(Stream s)
        {
            while (s.Position < s.Length)
            {
                if ((s.Position & 1) == 1)
                    s.ReadByte(); // Alignment

                var type = s.ReadASCII(4);
                switch (type)
                {
                    case "RIFF":
                        FileSize = s.ReadInt32();
                        Format = s.ReadASCII(4);
                        if (Format != "WAVE")
                            throw new NotSupportedException("Not a canonical WAVE file.");
                        break;
                    case "fmt ":
                        FmtChunkSize = s.ReadInt32();
                        if (FmtChunkSize != 16)
                            throw new NotSupportedException("{0} fmt chunk size is not a supported encoding scheme.".F(FmtChunkSize));
                        AudioFormat = s.ReadInt16();
                        if (AudioFormat != 1)
                            throw new NotSupportedException("Non-PCM compression is not supported.");
                        Channels = s.ReadInt16();
                        SampleRate = s.ReadInt32();
                        ByteRate = s.ReadInt32();
                        BlockAlign = s.ReadInt16();
                        BitsPerSample = s.ReadInt16();
                        break;
                    case "data":
                        DataSize = s.ReadInt32();
                        RawOutput = s.ReadBytes(DataSize);
                        break;
                    default:
                        // Ignore unknown chunks
                        var chunkSize = s.ReadInt32();
                        s.ReadBytes(chunkSize);
                        break;
                }
            }
        }
示例#16
0
        public override byte[] Decompress(Stream s, int dataLen, uint key)
        {
            var sb = new List<byte>();
            var decoder = new LZWDecoder();

            while (sb.Count < dataLen)
            {
                sb.AddRange(decoder.Decode(s.ReadBytes(s.ReadInt32())));
            }

            return sb.ToArray();
        }
示例#17
0
		public static void Decode (Stream stream, BitArray val)
		{

			Debug.Assert (stream != null, "Stream is null");
			Int16 num = Int16EncodingLE.Decode (stream);
			byte[] buffer = new byte[stream.Length-2];
			buffer = stream.ReadBytes (2, ((uint)stream.Length - 2));
//			if (EncodingHelpers.Endian != EndianTypes.LittleEndian)
//				EncodingHelpers.ReverseBytes (buffer, 2, buffer.Length);
			val = new BitArray (buffer);
			val.Length = num;
		}
示例#18
0
        public static bool HandlePacket( ServerBase sender, Stream stream )
        {
            UInt16 id = BitConverter.ToUInt16( stream.ReadBytes( 2 ), 0 );

            if ( stTypeIDs.Count <= id )
                throw new Exception( "Unknown packet type recieved" );

            ServerPacketType type = stTypeIDs[ id ];

            if ( type == null )
                throw new Exception( "Unknown packet type recieved" );

            return type.PacketHandler( sender, type, stream );
        }
示例#19
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]];
			}
示例#20
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;
            }
        }
示例#21
0
文件: gim.cs 项目: memerdot/puyotools
        /* Unpack a GIM into a Bitmap */
        public override Bitmap Unpack(ref Stream data)
        {
            /* Convert the GIM to an image */
            try
            {
                GimFile imageInput  = new GimFile(data.ReadBytes(0, (int)data.Length));
                ImgFile imageOutput = new ImgFile(imageInput.GetDecompressedData(), imageInput.GetWidth(), imageInput.GetHeight(), ImageFormat.Png);

                return new Bitmap(new MemoryStream(imageOutput.GetCompressedData()));
            }
            catch
            {
                return null;
            }
        }
示例#22
0
 /// <summary>发送数据流。</summary>
 /// <param name="stream"></param>
 protected override void OnSend(Stream stream)
 {
     lock (this)
     {
         var client = Client;
         if (!_hasSetAsync)
         {
             client.UploadDataCompleted += new UploadDataCompletedEventHandler(client_UploadDataCompleted);
             client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
         }
         var data = stream.ReadBytes();
         if (data.Length < 128)
             client.DownloadDataAsync(new Uri(Uri.ToString() + "?" + DataHelper.ToHex(data)));
         else
             client.UploadDataAsync(Uri, data);
     }
 }
示例#23
0
        public void UnpackAll(Stream stream, string baseDirectory)
        {
            int memoryGateSize = (int)((GC.GetTotalMemory(false) + estimatedMaximumBufferSize) / (1024*1024)) + 1;
            Log.Write("Checking if we have {0} MB of RAM available... ", memoryGateSize);
            try
            {
                new MemoryFailPoint(memoryGateSize);
                Log.Success("ok");
            }
            catch(InsufficientMemoryException)
            {
                Log.Warning("failed. There might be errors.");
            }

            var digits = (int) Math.Ceiling(Math.Log10(fileCount));
            string mask = "{0,"+digits+"}/{1}: {2} ";
            for (int i = 0; i < fileEntry.Count; i++)
            {
                var entry = fileEntry[i];
                Log.Write(mask, i+1, fileCount, entry.filename);
                try
                {
                    stream.Seek(entry.offset, SeekOrigin.Begin);
                    var localOffset = stream.ReadInt32();
                    stream.Seek(entry.offset + localOffset, SeekOrigin.Begin);

                    var outFilename = Path.Combine(baseDirectory, entry.filename);
                    var outDirectory = Path.GetDirectoryName(outFilename);
                    if (!Directory.Exists(outDirectory))
                        Directory.CreateDirectory(outDirectory);

                    var input = stream.ReadBytes((int) (entry.compressedLength - localOffset));
                    var output = new byte[entry.decompressedLength];
                    LZF.Decompress(input, input.Length, output, output.Length);
                    using (var outStream = File.OpenWrite(outFilename))
                        outStream.Write(output, 0, output.Length);

                    Log.Success("ok");
                }
                catch (Exception e)
                {
                    Log.Error("failed: " + e.Message);
                }

            }
        }
示例#24
0
        public static bool HandlePacket( ClientBase sender, Stream stream )
        {
            UInt16 id = BitConverter.ToUInt16( stream.ReadBytes( 2 ), 0 );

            if ( stNextID <= id )
                throw new Exception( "Unknown packet type recieved" );

            ClientPacketType type = stTypeIDs[ id ];
            return type.PacketHandler( sender, type, stream );
        }
示例#25
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();
        }
示例#26
0
        // VQA Frame
        public void DecodeVQFR(Stream s)
        {
            while (true)
            {
                // Chunks are aligned on even bytes; may be padded with a single null
                if (s.Peek() == 0) s.ReadByte();
                var type = s.ReadASCII(4);
                var subchunkLength = (int)int2.Swap(s.ReadUInt32());

                switch(type)
                {
                    // Full frame-modifier
                    case "CBFZ":
                        Format80.DecodeInto(s.ReadBytes(subchunkLength), cbf);
                    break;
                    case "CBF0":
                        cbf = s.ReadBytes(subchunkLength);
                    break;

                    // frame-modifier chunk
                    case "CBP0":
                    case "CBPZ":
                        // Partial buffer is full; dump and recreate
                        if (cbChunk == cbParts)
                        {
                            if (type == "CBP0")
                                cbf = (byte[])cbp.Clone();
                            else
                                Format80.DecodeInto(cbp, cbf);

                            cbOffset = cbChunk = 0;
                        }

                        var bytes = s.ReadBytes(subchunkLength);
                        bytes.CopyTo(cbp,cbOffset);
                        cbOffset += subchunkLength;
                        cbChunk++;
                    break;

                    // Palette
                    case "CPL0":
                        for (var i = 0; i < numColors; i++)
                        {
                            var r = (byte)(s.ReadUInt8() << 2);
                            var g = (byte)(s.ReadUInt8() << 2);
                            var b = (byte)(s.ReadUInt8() << 2);
                            palette[i] = (uint)((255 << 24) | (r << 16) | (g << 8) | b);
                        }
                    break;

                    // Frame data
                    case "VPTZ":
                        Format80.DecodeInto(s.ReadBytes(subchunkLength), origData);
                        // This is the last subchunk
                        return;
                    default:
                        throw new InvalidDataException("Unknown sub-chunk {0}".F(type));
                }
            }
        }
示例#27
0
        /// <summary>
        /// Takes compressed archived image data and returns the raw image data
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte[] DecompressTex(Stream archiveStream, int offset, int uncSize, int cprSize)
        {
            int pos = 0;
            archiveStream.Seek(offset, SeekOrigin.Begin);
            int magicNumber = archiveStream.ReadValueS32();
            pos += 4;
            if (magicNumber != -1641380927)
            {
                throw new FormatException("Magic Number is not correct. Invalid archive data");
            }
            int blockSize = archiveStream.ReadValueS32();
            pos += 4;
            int readCprSize = archiveStream.ReadValueS32(); //Archive cprSize doesn't include header size
            pos += 4;
            int uncReadSize = archiveStream.ReadValueS32();
            if (uncReadSize != uncSize)
            {
                throw new FormatException("Uncompressed data sizes don't match. Read: " + uncReadSize + ", expected: " + uncSize);
            }
            pos += 4;
            int noChunks = (uncSize + blockSize - 1) / blockSize;

            CompressedChunkBlock[] chunks = new CompressedChunkBlock[noChunks];
            for (int i = 0; i < noChunks; i++)
            {
                CompressedChunkBlock chunk = new CompressedChunkBlock();
                chunk.cprSize = archiveStream.ReadValueS32();
                chunk.uncSize = archiveStream.ReadValueS32();
                chunk.rawData = new byte[chunk.cprSize];
                pos += 8;
                chunks[i] = chunk;
            }
            if (readCprSize + pos != cprSize)
            {
                throw new FormatException("Compressed data sizes don't match. Invalid archive data");
            }
            byte[] rawData = new byte[readCprSize];
            rawData = archiveStream.ReadBytes(readCprSize);
            archiveStream.Close();
            using (MemoryStream data = new MemoryStream(rawData))
            {
                for (int i = 0; i < noChunks; i++)
                {
                    chunks[i].rawData = data.ReadBytes(chunks[i].cprSize);
                }
            }

            byte[] imgBuffer = new byte[uncSize];
            int resultPos = 0;

            for (int i = 0; i < noChunks; i++)
            {
                CompressedChunkBlock chunk = chunks[i];
                byte[] tempResult = new byte[chunk.uncSize];
                try
                {
                    LZO1X.Decompress(chunk.rawData, tempResult);
                }
                catch
                {
                    throw new Exception("LZO decompression failed!");
                }
                Buffer.BlockCopy(tempResult, 0, imgBuffer, resultPos, chunk.uncSize);
                resultPos += chunk.uncSize;
            }

            return imgBuffer;
        }
示例#28
0
        public MemoryStream DecompressPCC(Stream raw, IPCCObject pcc)
        {
            raw.Seek(pcc.header.Length, SeekOrigin.Begin);
            int pos = 4;
            pcc.NumChunks = raw.ReadValueS32();
            List<Chunk> Chunks = new List<Chunk>();

            //DebugOutput.PrintLn("Reading chunk headers...");
            for (int i = 0; i < pcc.NumChunks; i++)
            {
                Chunk c = new Chunk();
                c.uncompressedOffset = raw.ReadValueS32();
                c.uncompressedSize = raw.ReadValueS32();
                c.compressedOffset = raw.ReadValueS32();
                c.compressedSize = raw.ReadValueS32();
                c.Compressed = new byte[c.compressedSize];
                c.Uncompressed = new byte[c.uncompressedSize];
                //DebugOutput.PrintLn("Chunk " + i + ", compressed size = " + c.compressedSize + ", uncompressed size = " + c.uncompressedSize);
                //DebugOutput.PrintLn("Compressed offset = " + c.compressedOffset + ", uncompressed offset = " + c.uncompressedOffset);
                Chunks.Add(c);
            }

            //DebugOutput.PrintLn("\tRead Chunks...");
            int count = 0;
            for (int i = 0; i < Chunks.Count; i++)
            {
                Chunk c = Chunks[i];
                raw.Seek(c.compressedOffset, SeekOrigin.Begin);
                c.Compressed = raw.ReadBytes(c.compressedSize);

                ChunkHeader h = new ChunkHeader();
                h.magic = BitConverter.ToInt32(c.Compressed, 0);
                if (h.magic != -1641380927)
                    throw new FormatException("Chunk magic number incorrect");
                h.blocksize = BitConverter.ToInt32(c.Compressed, 4);
                h.compressedsize = BitConverter.ToInt32(c.Compressed, 8);
                h.uncompressedsize = BitConverter.ToInt32(c.Compressed, 12);
                //DebugOutput.PrintLn("Chunkheader read: Magic = " + h.magic + ", Blocksize = " + h.blocksize + ", Compressed Size = " + h.compressedsize + ", Uncompressed size = " + h.uncompressedsize);
                pos = 16;
                int blockCount = (h.uncompressedsize % h.blocksize == 0)
                    ?
                    h.uncompressedsize / h.blocksize
                    :
                    h.uncompressedsize / h.blocksize + 1;
                List<Block> BlockList = new List<Block>();
                //DebugOutput.PrintLn("\t\t" + count + " Read Blockheaders...");
                for (int j = 0; j < blockCount; j++)
                {
                    Block b = new Block();
                    b.compressedsize = BitConverter.ToInt32(c.Compressed, pos);
                    b.uncompressedsize = BitConverter.ToInt32(c.Compressed, pos + 4);
                    //DebugOutput.PrintLn("Block " + j + ", compressed size = " + b.compressedsize + ", uncompressed size = " + b.uncompressedsize);
                    pos += 8;
                    BlockList.Add(b);
                }
                int outpos = 0;
                //DebugOutput.PrintLn("\t\t" + count + " Read and decompress Blocks...");
                foreach (Block b in BlockList)
                {
                    byte[] datain = new byte[b.compressedsize];
                    byte[] dataout = new byte[b.uncompressedsize];
                    for (int j = 0; j < b.compressedsize; j++)
                        datain[j] = c.Compressed[pos + j];
                    pos += b.compressedsize;

                    try
                    {
                        LZO1X.Decompress(datain, dataout);
                    }
                    catch
                    {
                        throw new Exception("LZO decompression failed!");
                    }
                    for (int j = 0; j < b.uncompressedsize; j++)
                        c.Uncompressed[outpos + j] = dataout[j];
                    outpos += b.uncompressedsize;
                }
                c.header = h;
                c.blocks = BlockList;
                count++;
                Chunks[i] = c;
            }

            MemoryStream result = new MemoryStream();
            foreach (Chunk c in Chunks)
            {
                result.Seek(c.uncompressedOffset, SeekOrigin.Begin);
                result.WriteBytes(c.Uncompressed);
            }

            return result;
        }
        public void Read(Stream stream)
        {
            using (var ms = new MemoryStream())
            {
                byte[] signature1Bytes = stream.ReadBytes(4);
                byte[] signature2Bytes = stream.ReadBytes(4);

                uint signature1 = signature1Bytes.ToUInt();
                uint signature2 = signature2Bytes.ToUInt();

                bool pass = signature1 == Signature1 && signature2 == Signature2;
                if (!pass)
                    throw new FormatException("Database has incorrect file signature");

                byte[] versionBytes = stream.ReadBytes(4);
                uint version = versionBytes.ToUInt();

                if ((version & Mask) > (Version & Mask))
                    throw new FormatException("The file version is unsupported");

                ms.Write(signature1Bytes);
                ms.Write(signature2Bytes);
                ms.Write(versionBytes);

                while(this.ReadNext(stream, ms))
                {
                    // Add Logging Statement
                }

                this.Hash = ms.ToSHA256Hash();
            }
        }
        protected bool ReadNext(Stream input, Stream output)
        {
            int nextByte = input.ReadByte();
            if (nextByte == -1)
                throw new EndOfStreamException();

            byte fieldIdByte = (byte)nextByte;
            output.WriteByte(fieldIdByte);

            KeePassFileHeaderFields field = (KeePassFileHeaderFields)fieldIdByte;

            byte[] sizeBytes = input.ReadBytes(2);
            ushort size = sizeBytes.ToUShort();
           

            byte[] data = null;
            if (size > 0)
                data = input.ReadBytes(size);

            output.Write(sizeBytes);
            output.Write(data);

            switch (field)
            {
                case KeePassFileHeaderFields.EndOfHeader:
                    return false;

                case KeePassFileHeaderFields.DatabaseCipherId:
                    this.DatabaseCipherId = data;
                    break;
                case KeePassFileHeaderFields.DatabaseCompression:
                    if (data.Length < 1)
                        throw new Exception("Invalid Header");
                    byte flag = data[0];
                    this.DatabaseCompression = data[0];
                    break;
                case KeePassFileHeaderFields.DatabaseCipherKeySeed:
                    this.DatabaseCipherKeySeed = data;
                    break;
                case KeePassFileHeaderFields.MasterKeyHashSeed:
                    this.MasterKeyHashKey = data;
                    break;
                case KeePassFileHeaderFields.MasterKeyHashRounds:
                    this.MasterKeyHashRounds = data.ToInt32();
                    break;
                case KeePassFileHeaderFields.DatabaseCipherIV:
                    this.DatabaseCipherIV = data;
                    break;
                case KeePassFileHeaderFields.RandomBytesCryptoKey:
                    this.RandomByteGeneratorCryptoKey = data;
                   
                    break;
                case KeePassFileHeaderFields.HeaderByteMark:
                    this.HeaderByteMarks = data;
                    break;

                case KeePassFileHeaderFields.RandomBytesCryptoType:
                    if (data.Length < 1)
                        throw new Exception("Invalid Header");
                    this.RandomByteGeneratorCryptoType = data[0];
                    break;
                default:
                    return false;
          
            }

            return true;
        }