Ejemplo n.º 1
0
        private static void WriteBackrefLength(CrilaylaBitstream output, long len)
        {
            len -= 3;

            if (len < 3)
            {
                output.PushBits2((uint)len);
                return;
            }
            output.PushBits2(3);
            len -= 3;

            if (len < 7)
            {
                output.PushBits3((uint)len);
                return;
            }
            output.PushBits3(7);
            len -= 7;

            if (len < 31)
            {
                output.PushBits5((uint)len);
                return;
            }
            output.PushBits5(31);
            len -= 31;

            while (true)
            {
                if (len < 255)
                {
                    output.PushBits8((uint)len);
                    return;
                }
                output.PushBits8(255);
                len -= 255;
            }
        }
Ejemplo n.º 2
0
        public static long compress(Stream infile, long offset, long length, Stream outfile)
        {
            if (length <= 0x100)
            {
                throw new Exception("data too short, can't compress this");
            }

            // read bytes to be compressed into buffers
            long dataLength = length - 0x100;

            byte[] uncompressedData = new byte[0x100];
            byte[] input            = new byte[dataLength];
            Util.get_bytes_seek(offset, infile, uncompressedData, 0x100);
            Util.get_bytes(infile, input, dataLength);
            long currentPosition = input.LongLength - 1;

            // compress
            CrilaylaBitstream  output = new CrilaylaBitstream();
            BackrefFinderCache cache  = new BackrefFinderCache(input.LongLength);

            while (currentPosition >= 0)
            {
                ushort backrefPos;
                long   backrefLen;
                cache.GetBackref(input, currentPosition, out backrefPos, out backrefLen);
                if (backrefLen >= 3)
                {
                    output.PushBit(1);
                    output.PushBits13(backrefPos);
                    WriteBackrefLength(output, backrefLen);
                    currentPosition -= backrefLen;
                }
                else
                {
                    output.PushBit(0);
                    output.PushBits8(input[currentPosition]);
                    --currentPosition;
                }
            }

            MemoryStream ms = output.Finalize();
            long         compressedLength = ms.Length;

            // write header
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 56) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 48) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 40) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 32) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 24) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 16) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig >> 8) & 0xff));
            outfile.WriteByte((byte)((CpkUncompress.CRILAYLA_sig) & 0xff));
            outfile.WriteByte((byte)((dataLength) & 0xff));
            outfile.WriteByte((byte)((dataLength >> 8) & 0xff));
            outfile.WriteByte((byte)((dataLength >> 16) & 0xff));
            outfile.WriteByte((byte)((dataLength >> 24) & 0xff));
            outfile.WriteByte((byte)((compressedLength) & 0xff));
            outfile.WriteByte((byte)((compressedLength >> 8) & 0xff));
            outfile.WriteByte((byte)((compressedLength >> 16) & 0xff));
            outfile.WriteByte((byte)((compressedLength >> 24) & 0xff));

            // write compressed data
            byte[] buffer    = ms.GetBuffer();
            long   bufferpos = compressedLength - 1;

            while (bufferpos >= 0)
            {
                outfile.WriteByte(buffer[bufferpos]);
                --bufferpos;
            }

            // write uncompressed data
            outfile.Write(uncompressedData, 0, 0x100);

            // and we're done!
            return(compressedLength + 0x110);
        }