示例#1
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);
                }
        }
示例#2
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_)
            });
        }
示例#3
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);
                }
        }
示例#4
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);
                }
        }