예제 #1
0
        IBinaryStream OpenGamStream(IBinaryStream input)
        {
            input.Position = 8;
            var unpacked = new PackedStream <GamDecompressor> (input.AsStream, true);

            return(new BinaryStream(unpacked, input.Name));
        }
예제 #2
0
파일: ImageMGO.cs 프로젝트: zxc120/GARbro
        }                                                                 // 'MGOB'

        public override ImageMetaData ReadMetaData(IBinaryStream file)
        {
            var header = file.ReadHeader(12);

            if (header.ToInt32(4) != 5)
            {
                return(null);
            }
            using (var lzss = new PackedStream <LzssDecompressor> (file.AsStream, true))
                using (var stream = new SeekableStream(lzss))
                    using (var mgo = new BinaryStream(stream, file.Name))
                    {
                        var name = mgo.ReadCString();
                        mgo.Position = ((int)(mgo.Position - 1) & ~3) + 4;
                        int count = mgo.ReadInt32();
                        mgo.Seek(count * 0x10, SeekOrigin.Current);
                        int bmp_position = (int)mgo.Position;
                        var bmp_header   = mgo.ReadBytes(0x36);
                        if (!bmp_header.AsciiEqual("BM"))
                        {
                            return(null);
                        }
                        return(new MgoMetaData {
                            Width = bmp_header.ToUInt32(0x12),
                            Height = bmp_header.ToUInt32(0x16),
                            BPP = bmp_header.ToInt32(0x1C),
                            UnpackedSize = header.ToInt32(8),
                            BmpOffset = bmp_position,
                        });
                    }
        }
예제 #3
0
        internal IBinaryStream OpenCompressed(IBinaryStream file)
        {
            Stream input = new StreamRegion(file.AsStream, 2, true);

            input = new PackedStream <CpLzssDecompressor> (input);
            return(new BinaryStream(input, file.Name));
        }
        public async Task SimpleMessageTestAsync()
        {
            using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.In))
                using (var pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, pipeServer.ClientSafePipeHandle))
                {
                    var    ps    = new PackedStream(pipeServer, pipeClient);
                    var    rdn   = new Random();
                    var    data  = new byte[rdn.Next(10, 1024)];
                    byte[] nData = null;
                    var    mre   = new ManualResetEvent(false);
                    rdn.NextBytes(data);

                    ps.DataReceived += (s, d) =>
                    {
                        nData = d.MemoryStream.ToArray();

                        mre.Set();
                    };

                    await ps.WriteAsync(new MemoryStream(data));

                    mre.WaitOne();

                    Assert.AreEqual(data.Length, nData.Length);

                    for (var i = 0; i < data.Length; i++)
                    {
                        Assert.AreEqual(data[i], nData[i]);
                    }
                }
        }
        public void NoEventWatch()
        {
            using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.In))
                using (var pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, pipeServer.ClientSafePipeHandle))
                {
                    var ps = new PackedStream(pipeServer, pipeClient);

                    var data = new byte[] { 0, 1, 2 };
                    ps.Write(new MemoryStream(data));
                }
        }
예제 #6
0
파일: ImageMGO.cs 프로젝트: zxc120/GARbro
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var meta = (MgoMetaData)info;

            file.Position = 12;
            using (var lzss = new PackedStream <LzssDecompressor> (file.AsStream, true))
            {
                var header = new byte[meta.BmpOffset];
                lzss.Read(header, 0, header.Length);
                var decoder = new BmpBitmapDecoder(lzss,
                                                   BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                var frame = decoder.Frames[0];
                frame.Freeze();
                return(new ImageData(frame, info));
            }
        }
        public void Disconnected()
        {
            var mre = new ManualResetEvent(false);

            using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.In))
                using (var pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, pipeServer.ClientSafePipeHandle))
                {
                    var ps = new PackedStream(pipeServer, pipeClient);

                    ps.Disconected += (s, e) => mre.Set();
                }

            if (!mre.WaitOne(1000))
            {
                Assert.Fail();
            }
        }
예제 #8
0
파일: ArcBK.cs 프로젝트: zxc120/GARbro
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            Stream input = arc.File.CreateStream(entry.Offset, entry.Size);
            var    pent  = entry as PackedEntry;

            if (null == pent || !pent.IsPacked)
            {
                return(input);
            }
            input = new PackedStream <LzBitsDecompressor> (input);
            input = new LimitStream(input, pent.UnpackedSize);
            if (entry.Name.HasExtension(".gpa"))
            {
                input = new XoredStream(input, 0xFF);
            }
            return(input);
        }
예제 #9
0
파일: ArcBK.cs 프로젝트: zxc120/GARbro
        public override ArcFile TryOpen(ArcView file)
        {
            uint index_offset = file.View.ReadUInt32(0);
            uint index_size   = file.View.ReadUInt32(4);

            if (index_offset + index_size != file.MaxOffset)
            {
                return(null);
            }
            using (var input = file.CreateStream(index_offset, index_size))
                using (var packed = new PackedStream <LzBitsDecompressor> (input))
                    using (var index = new BinaryStream(packed, file.Name))
                    {
                        int count = index.ReadInt32();
                        if (!IsSaneCount(count))
                        {
                            return(null);
                        }
                        var dir = new List <Entry> (count);
                        for (int i = 0; i < count; ++i)
                        {
                            uint offset        = index.ReadUInt32();
                            uint size          = index.ReadUInt32();
                            uint unpacked_size = index.ReadUInt32();
                            var  name          = index.ReadCString(0x104);
                            var  entry         = FormatCatalog.Instance.Create <PackedEntry> (name);
                            entry.Offset       = offset;
                            entry.Size         = size;
                            entry.UnpackedSize = unpacked_size;
                            if (!entry.CheckPlacement(file.MaxOffset))
                            {
                                return(null);
                            }
                            if (name.HasExtension(".gpt"))
                            {
                                entry.Type = "image";
                            }
                            entry.IsPacked = entry.Size != entry.UnpackedSize;
                            dir.Add(entry);
                        }
                        return(new ArcFile(file, this, dir));
                    }
        }
예제 #10
0
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            var    aent = (AgsiEntry)entry;
            var    aarc = arc as AgsiArchive;
            Stream input;

            if (!aent.IsEncrypted)
            {
                input = arc.File.CreateStream(entry.Offset, entry.Size);
            }
            else if (aarc != null && aarc.Key != null)
            {
                input = OpenEncryptedEntry(aarc, aent);
            }
            else
            {
                return(base.OpenEntry(arc, entry));
            }
            switch (aent.Method)
            {
            case 0: // no compression
            case 3:
                break;

            case 1: // RLE compression
            case 4:
                input = new PackedStream <RleDecompressor> (input, new RleDecompressor((int)aent.UnpackedSize));
                break;

            case 2: // LZSS bit stream
            case 5:
                input = new PackedStream <LzBitStream> (input, new LzBitStream((int)aent.UnpackedSize));
                break;

            case 6: // LZSS compression
            case 7:
                input = new LzssStream(input);
                break;
            }
            return(input);
        }
예제 #11
0
파일: ArcIDA.cs 프로젝트: zxc120/GARbro
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            var ient = entry as IdaEntry;

            if (null == ient || 0 == ient.Flags)
            {
                return(base.OpenEntry(arc, entry));
            }
            Stream input = arc.File.CreateStream(entry.Offset, entry.Size);

            if (0 != (ient.Flags & 0xB))
            {
                input = DecryptEntry(input, ient);
            }
            if (0 != (ient.Flags & 4))
            {
                input = new PackedStream <RleDecompressor> (input);
            }
            if (0 != (ient.Flags & 0x10))
            {
                input = new ZLibStream(input, CompressionMode.Decompress);
            }
            return(input);
        }
예제 #12
0
        internal PackedStream Compress(Stream inputStream, SevenZipProgressProvider progressProvider)
        {
            // Compression method
            if (!Method.HasValue || !SevenZipMethods.Lookup.ContainsKey(Method.Value))
            {
                throw new SevenZipException("Undefined compression method.");
            }
            var MethodID = SevenZipMethods.Lookup[Method.Value];

            // create compressed stream information structure
            var ps = new PackedStream()
            {
                NumStreams = 1,
                Sizes      = new UInt64[1] {
                    0
                },
                CRCs = new UInt32?[1] {
                    null
                },
                Folder = new SevenZipHeader.Folder()
                {
                    NumCoders  = 1,
                    CodersInfo = new SevenZipHeader.CoderInfo[1]
                    {
                        new SevenZipHeader.CoderInfo()
                        {
                            Attributes    = (Byte)MethodID.Raw.Length,
                            CodecId       = MethodID.Raw.ToArray(),
                            NumInStreams  = 1,
                            NumOutStreams = 1
                        }
                    },
                    NumInStreamsTotal  = 1,
                    NumOutStreamsTotal = 1,
                    PackedIndices      = new UInt64[1] {
                        0
                    },
                    UnPackSizes = new UInt64[1] {
                        0
                    },
                    UnPackCRC = 0
                }
            };

            // remember current offsets
            long outStreamStartOffset = this.stream.Position;
            long inStreamStartOffset  = inputStream.Position;

            // encode while calculating CRCs
            using (var inCRCStream = new CRCStream(inputStream))
                using (var outCRCStream = new CRCStream(stream))
                {
                    LZMA.CLzmaEnc      encoder      = LZMA.LzmaEnc_Create(LZMA.ISzAlloc.SmallAlloc);
                    LZMA.CLzmaEncProps encoderProps = LZMA.CLzmaEncProps.LzmaEncProps_Init();
                    LZMA.CSeqOutStream outputHelper;
                    LZMA.CSeqInStream  inputHelper;
                    LZMA.SRes          res = LZMA.SZ_OK;

                    // prepare encoder settings
                    res = encoder.LzmaEnc_SetProps(encoderProps);
                    if (res != LZMA.SZ_OK)
                    {
                        throw new SevenZipException("Error setting LZMA encoder properties.");
                    }
                    byte[] properties         = new byte[LZMA.LZMA_PROPS_SIZE];
                    long   binarySettingsSize = LZMA.LZMA_PROPS_SIZE;
                    res = encoder.LzmaEnc_WriteProperties(properties, ref binarySettingsSize);
                    if (res != LZMA.SZ_OK)
                    {
                        throw new SevenZipException("Error writing LZMA encoder properties.");
                    }
                    if (binarySettingsSize != LZMA.LZMA_PROPS_SIZE)
                    {
                        throw new NotSupportedException();
                    }

                    // read/write helpers
                    outputHelper = new LZMA.CSeqOutStream(
                        (P <byte> buf, long sz) => {
                        outCRCStream.Write(buf.mBuffer, buf.mOffset, checked ((int)sz));
                    });

                    inputHelper = new LZMA.CSeqInStream(
                        (P <byte> buf, long sz) => {
                        return(inCRCStream.Read(buf.mBuffer, buf.mOffset, checked ((int)sz)));
                    });

                    // encode
                    res = encoder.LzmaEnc_Encode(outputHelper, inputHelper, progressProvider, LZMA.ISzAlloc.SmallAlloc, LZMA.ISzAlloc.BigAlloc);
                    if (res != LZMA.SZ_OK)
                    {
                        throw new InvalidOperationException();
                    }

                    // cleanup
                    encoder.LzmaEnc_Destroy(LZMA.ISzAlloc.SmallAlloc, LZMA.ISzAlloc.BigAlloc);

                    // keep settings in header
                    ps.Folder.CodersInfo[0].Attributes    |= (Byte)SevenZipHeader.CoderInfo.AttrHasAttributes;
                    ps.Folder.CodersInfo[0].Properties     = properties.ToArray();
                    ps.Folder.CodersInfo[0].PropertiesSize = (UInt64)ps.Folder.CodersInfo[0].Properties.Length;

                    // store sizes and checksums
                    ps.Folder.UnPackSizes[0] = (UInt64)(inputStream.Position - inStreamStartOffset);
                    ps.Folder.UnPackCRC      = inCRCStream.Result;
                    ps.Sizes[0] = (UInt64)(this.stream.Position - outStreamStartOffset);
                    ps.CRCs[0]  = outCRCStream.Result;

                    // handle progress offsets (in case compressor is called multiple times, with non-solid archives for instance)
                    if (progressProvider != null)
                    {
                        progressProvider.IncreaseOffsetBy((long)ps.Folder.UnPackSizes[0], (long)ps.Sizes[0]);
                        progressProvider.SetProgress(0, 0);
                    }
                }

            return(ps);
        }