示例#1
0
        public void ServiceTest()
        {
            Stream      serverPeerStream = new Regulus.Remote.Standalone.Stream();
            IStreamable serverStream     = serverPeerStream;
            IStreamable clientStream     = new ReverseStream(serverPeerStream);

            IBinderProvider entry = NSubstitute.Substitute.For <IBinderProvider>();
            IGpiA           gpia  = new SoulGpiA();

            entry.AssignBinder(NSubstitute.Arg.Do <IBinder>(binder => binder.Bind <IGpiA>(gpia)), NSubstitute.Arg.Any <object>());

            Serialization.ISerializer serializer = new Regulus.Serialization.Dynamic.Serializer();
            IProtocol protocol = ProtocolHelper.CreateProtocol(serializer);

            Soul.IService service   = new Regulus.Remote.Soul.Service(entry, protocol);
            IAgent        agent     = new Regulus.Remote.Ghost.Agent(protocol) as Ghost.IAgent;
            IGpiA         ghostGpia = null;


            service.Join(serverStream);
            agent.Start(clientStream);
            agent.QueryNotifier <IGpiA>().Supply += gpi => ghostGpia = gpi;

            while (ghostGpia == null)
            {
                agent.Update();
            }

            agent.Stop();
            service.Leave(serverStream);

            IDisposable disposable = service;

            disposable.Dispose();
        }
示例#2
0
        private void ExportDDS(IExportContainer container, Stream destination, Stream source, long length)
        {
            DDSConvertParameters @params = new DDSConvertParameters()
            {
                DataLength          = length,
                MipMapCount         = MipCount,
                Width               = Width,
                Height              = Height,
                IsPitchOrLinearSize = DDSIsPitchOrLinearSize,
                PixelFormatFlags    = DDSPixelFormatFlags,
                FourCC              = (DDSFourCCType)DDSFourCC,
                RGBBitCount         = DDSRGBBitCount,
                RBitMask            = DDSRBitMask,
                GBitMask            = DDSGBitMask,
                BBitMask            = DDSBBitMask,
                ABitMask            = DDSABitMask,
                Caps = DDSCaps(container.Version),
            };

            if (IsSwapBytes(container.Platform))
            {
                using (ReverseStream reverse = new ReverseStream(source, source.Position, length, true))
                {
                    DDSConverter.ExportDDS(destination, reverse, @params);
                }
            }
            else
            {
                DDSConverter.ExportDDS(destination, source, @params);
            }
        }
示例#3
0
        public void Encode(Stream input, Stream output)
        {
            var matches = _matchParser.ParseMatches(input).ToArray();

            var compressedLength = PreCalculateCompressedLength(input.Length, matches);

            var block = new Block();

            using (var inputReverseStream = new ReverseStream(input, input.Length))
                using (var reverseOutputStream = new ReverseStream(output, compressedLength))
                {
                    foreach (var match in matches)
                    {
                        while (match.Position > inputReverseStream.Position)
                        {
                            if (block.codeBlockPosition == 0)
                            {
                                WriteAndResetBuffer(reverseOutputStream, block);
                            }

                            block.codeBlockPosition--;
                            block.buffer[block.bufferLength++] = (byte)inputReverseStream.ReadByte();
                        }

                        var byte1 = ((byte)(match.Length - 3) << 4) | (byte)((match.Displacement - 3) >> 8);
                        var byte2 = match.Displacement - 3;

                        if (block.codeBlockPosition == 0)
                        {
                            WriteAndResetBuffer(reverseOutputStream, block);
                        }

                        block.codeBlock |= (byte)(1 << --block.codeBlockPosition);
                        block.buffer[block.bufferLength++] = (byte)byte1;
                        block.buffer[block.bufferLength++] = (byte)byte2;

                        inputReverseStream.Position += match.Length;
                    }

                    // Write any data after last match, to the buffer
                    while (inputReverseStream.Position < inputReverseStream.Length)
                    {
                        if (block.codeBlockPosition == 0)
                        {
                            WriteAndResetBuffer(reverseOutputStream, block);
                        }

                        block.codeBlockPosition--;
                        block.buffer[block.bufferLength++] = (byte)inputReverseStream.ReadByte();
                    }

                    // Flush remaining buffer to stream
                    WriteAndResetBuffer(reverseOutputStream, block);

                    output.Position = compressedLength;
                    WriteFooterInformation(input, output);
                }
        }
示例#4
0
        public void Encode(Stream input, Stream output, IEnumerable <Match> matches)
        {
            var matchArray       = matches.ToArray();
            var compressedLength = CalculateCompressedLength(input.Length, matchArray);

            var outputSize = ((compressedLength + 0xF) & ~0xF) + 0x10;

            using var inputReverseStream  = new ReverseStream(input, input.Length);
            using var outputReverseStream = new ReverseStream(output, outputSize);

            using var bw = new BinaryWriterX(outputReverseStream, true, ByteOrder.LittleEndian, BitOrder.MostSignificantBitFirst, 1);

            foreach (var match in matchArray)
            {
                // Write raw bytes
                while (match.Position < input.Length - inputReverseStream.Position)
                {
                    bw.WriteBit(false);
                    bw.WriteBits(inputReverseStream.ReadByte(), 8);
                }

                // Write match
                bw.WriteBit(true);
                bw.WriteBits(match.Displacement - 3, 13);
                WriteLength(bw, match.Length);

                inputReverseStream.Position += match.Length;
            }

            // Write remaining data
            while (inputReverseStream.Position < input.Length - SkipSize_)
            {
                bw.WriteBit(false);
                bw.WriteBits(inputReverseStream.ReadByte(), 8);
            }

            // Write raw start data
            input.Position = 0;
            var rawStart = new byte[0x100];

            input.Read(rawStart, 0, rawStart.Length);

            output.Position = output.Length;
            output.Write(rawStart, 0, rawStart.Length);

            // Write header
            using var outputBw = new BinaryWriterX(output, true);
            output.Position    = 0;
            outputBw.WriteType(new CrilaylaHeader
            {
                decompSize = (int)(input.Length - SkipSize_),
                compSize   = (int)(output.Length - 0x10 - SkipSize_)
            });
        }
示例#5
0
        public void Encode(Stream input, Stream output, IEnumerable <Match> matches)
        {
            var matchArray = matches.ToArray();

            var compressedLength = CalculateCompressedLength(input.Length, matchArray, out var lastRawLength);

            var block = new Block();

            using (var inputReverseStream = new ReverseStream(input, input.Length))
                using (var outputReverseStream = new ReverseStream(output, compressedLength + lastRawLength))
                {
                    foreach (var match in matchArray)
                    {
                        while (match.Position < input.Length - inputReverseStream.Position)
                        {
                            if (block.codeBlockPosition == 0)
                            {
                                WriteAndResetBuffer(outputReverseStream, block);
                            }

                            block.codeBlockPosition--;
                            block.buffer[block.bufferLength++] = (byte)inputReverseStream.ReadByte();
                        }

                        var byte1 = ((byte)(match.Length - 3) << 4) | (byte)((match.Displacement - 3) >> 8);
                        var byte2 = match.Displacement - 3;

                        if (block.codeBlockPosition == 0)
                        {
                            WriteAndResetBuffer(outputReverseStream, block);
                        }

                        block.codeBlock |= (byte)(1 << --block.codeBlockPosition);
                        block.buffer[block.bufferLength++] = (byte)byte1;
                        block.buffer[block.bufferLength++] = (byte)byte2;

                        inputReverseStream.Position += match.Length;
                    }

                    // Flush remaining buffer to stream
                    WriteAndResetBuffer(outputReverseStream, block);

                    // Write any data after last match as raw unbuffered data
                    while (inputReverseStream.Position < inputReverseStream.Length)
                    {
                        outputReverseStream.WriteByte((byte)inputReverseStream.ReadByte());
                    }

                    output.Position = compressedLength + lastRawLength;
                    WriteFooterInformation(input, output, lastRawLength);
                }
        }
示例#6
0
        public IEnumerable <Match> ParseMatches(Stream input)
        {
            if (FindOptions.PreBufferSize > 0)
            {
                input = new PreBufferStream(input, FindOptions.PreBufferSize);
            }

            if (FindOptions.SearchBackwards)
            {
                input = new ReverseStream(input, input.Length);
            }

            return(InternalParseMatches(input.ToArray(), FindOptions.PreBufferSize));
        }
示例#7
0
        /// <inheritdoc cref="ParseMatches"/>
        public IEnumerable <Match> ParseMatches(Stream input)
        {
            var toParse = input;

            if (FindOptions.PreBufferSize > 0)
            {
                toParse = FindOptions.SearchBackwards ?
                          new ConcatStream(input, new MemoryStream(new byte[FindOptions.PreBufferSize])) :
                          new ConcatStream(new MemoryStream(new byte[FindOptions.PreBufferSize]), input);
            }

            if (FindOptions.SearchBackwards)
            {
                toParse = new ReverseStream(toParse, toParse.Length);
            }

            return(InternalParseMatches(toParse.ToArray(), FindOptions.PreBufferSize));
        }
示例#8
0
        public void Decode(Stream input, Stream output)
        {
            using var br = new BinaryReaderX(input, true);

            var header = br.ReadType <CrilaylaHeader>();

            if (header.magic != "CRILAYLA" || header.magic == "\0\0\0\0\0\0\0\0")
            {
                throw new InvalidCompressionException("Crilayla");
            }

            // Copy raw part
            input.Position = input.Length - RawSize_;
            output.Write(br.ReadBytes(RawSize_), 0, RawSize_);

            // Decompress
            var compStream          = new SubStream(input, 0x10, input.Length - RawSize_ - 0x10);
            var reverseCompStream   = new ReverseStream(compStream, compStream.Length);
            var reverseOutputStream = new ReverseStream(output, header.decompSize + RawSize_);
            var circularBuffer      = new CircularBuffer(0x2002);

            using var reverseBr = new BinaryReaderX(reverseCompStream, ByteOrder.LittleEndian, BitOrder.MostSignificantBitFirst, 1);

            while (reverseOutputStream.Position < reverseOutputStream.Length - RawSize_)
            {
                if (!reverseBr.ReadBit())
                {
                    var value = reverseBr.ReadBits <byte>(8);

                    reverseOutputStream.WriteByte(value);
                    circularBuffer.WriteByte(value);
                    continue;
                }

                var displacement = reverseBr.ReadBits <short>(13) + 3;
                var length       = ReadLength(reverseBr) + 3;

                circularBuffer.Copy(reverseOutputStream, displacement, length);
            }
        }
示例#9
0
        public void Decode(Stream input, Stream output)
        {
            var buffer = new byte[4];

            input.Position = input.Length - 8;

            input.Read(buffer, 0, 4);
            var bufferTopAndBottom = _byteOrder == ByteOrder.LittleEndian ? buffer.GetInt32LittleEndian(0) : buffer.GetInt32BigEndian(0);

            input.Read(buffer, 0, 4);
            var decompressedOffset = _byteOrder == ByteOrder.LittleEndian ? buffer.GetInt32LittleEndian(0) : buffer.GetInt32BigEndian(0);

            var footerLength   = bufferTopAndBottom >> 24;
            var compressedSize = bufferTopAndBottom & 0xFFFFFF;

            using (var inputReverseStream = new ReverseStream(input, input.Length - footerLength))
                using (var outputReverseStream = new ReverseStream(output, input.Length + decompressedOffset))
                {
                    var endPosition = compressedSize - footerLength;
                    ReadCompressedData(inputReverseStream, outputReverseStream, endPosition);
                }
        }
示例#10
0
        public void Encode(Stream input, Stream output)
        {
            var matches = _matchParser.ParseMatches(input).ToArray();

            var compressedLength = PrecalculateCompressedLength(input.Length, matches);

            _codeBlock         = 0;
            _codeBlockPosition = 8;
            // We write all data backwards into the buffer; starting from last element down to first
            // We have 8 blocks; A block can be at max 2 bytes, defining a match
            _buffer       = new byte[8 * 2];
            _bufferLength = 0;

            using (var inputReverseStream = new ReverseStream(input, input.Length))
                using (var reverseOutputStream = new ReverseStream(output, compressedLength))
                {
                    foreach (var match in matches)
                    {
                        while (match.Position > inputReverseStream.Position)
                        {
                            if (_codeBlockPosition == 0)
                            {
                                WriteAndResetBuffer(reverseOutputStream);
                            }

                            _codeBlockPosition--;
                            _buffer[_bufferLength++] = (byte)inputReverseStream.ReadByte();
                        }

                        var byte1 = ((byte)(match.Length - 3) << 4) | (byte)((match.Displacement - 3) >> 8);
                        var byte2 = match.Displacement - 3;

                        if (_codeBlockPosition == 0)
                        {
                            WriteAndResetBuffer(reverseOutputStream);
                        }

                        _codeBlock |= (byte)(1 << --_codeBlockPosition);
                        _buffer[_bufferLength++] = (byte)byte1;
                        _buffer[_bufferLength++] = (byte)byte2;

                        inputReverseStream.Position += match.Length;
                    }

                    // Write any data after last match, to the buffer
                    while (inputReverseStream.Position < inputReverseStream.Length)
                    {
                        if (_codeBlockPosition == 0)
                        {
                            WriteAndResetBuffer(reverseOutputStream);
                        }

                        _codeBlockPosition--;
                        _buffer[_bufferLength++] = (byte)inputReverseStream.ReadByte();
                    }

                    // Flush remaining buffer to stream
                    WriteAndResetBuffer(reverseOutputStream);

                    output.Position = compressedLength;
                    WriteFooterInformation(input, output);
                }
        }