【4858.com】AppleLZF算法解析,Apple的LZF算法解析

By admin in 4858.com on 2019年5月4日

   
有关LZF算法的相干分析文书档案相比较少,不过Apple对LZF的开源,能够让大家对该算法举行二个简练的剖析。LZFSE
基于 Lempel-Ziv
,并选取了少于状态熵编码。LZF选拔类似lz7七和lzss的混合编码。使用3种“开首标识”来代表每段输出的数据串。

Apple的LZF算法解析,AppleLZF算法解析

   
有关LZF算法的相关分析文书档案比较少,可是Apple对LZF的开源,能够让大家对该算法实行贰个简短的辨析。LZFSE
基于 Lempel-Ziv
,并行使了点滴状态熵编码。LZF选用类似lz77和lzss的混合编码。使用三种“初始标识”来代表每段输出的数据串。

    接下去看一下开源的LZF算法的落成源码。

     壹.定义的全局字段:

       private readonly long[] _hashTable = new long[Hsize];

        private const uint Hlog = 14;

        private const uint Hsize = (1 << 14);

        private const uint MaxLit = (1 << 5);

        private const uint MaxOff = (1 << 13);

        private const uint MaxRef = ((1 << 8) + (1 << 3));

    二.用到LibLZF算法压缩数量:

        /// <summary>
        /// 使用LibLZF算法压缩数据
        /// </summary>
        /// <param name="input">需要压缩的数据</param>
        /// <param name="inputLength">要压缩的数据的长度</param>
        /// <param name="output">引用将包含压缩数据的缓冲区</param>
        /// <param name="outputLength">压缩缓冲区的长度(应大于输入缓冲区)</param>
        /// <returns>输出缓冲区中压缩归档的大小</returns>
        public int Compress(byte[] input, int inputLength, byte[] output, int outputLength)
        {
            Array.Clear(_hashTable, 0, (int)Hsize);
            uint iidx = 0;
            uint oidx = 0;
            var hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
            var lit = 0;
            for (; ; )
            {
                if (iidx < inputLength - 2)
                {
                    hval = (hval << 8) | input[iidx + 2];
                    long hslot = ((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1));
                    var reference = _hashTable[hslot];
                    _hashTable[hslot] = iidx;
                    long off;
                    if ((off = iidx - reference - 1) < MaxOff
                        && iidx + 4 < inputLength
                        && reference > 0
                        && input[reference + 0] == input[iidx + 0]
                        && input[reference + 1] == input[iidx + 1]
                        && input[reference + 2] == input[iidx + 2]
                        )
                    {
                        uint len = 2;
                        var maxlen = (uint)inputLength - iidx - len;
                        maxlen = maxlen > MaxRef ? MaxRef : maxlen;
                        if (oidx + lit + 1 + 3 >= outputLength)
                            return 0;
                        do
                            len++;
                        while (len < maxlen && input[reference + len] == input[iidx + len]);
                        if (lit != 0)
                        {
                            output[oidx++] = (byte)(lit - 1);
                            lit = -lit;
                            do
                                output[oidx++] = input[iidx + lit];
                            while ((++lit) != 0);
                        }
                        len -= 2;
                        iidx++;
                        if (len < 7)
                        {
                            output[oidx++] = (byte)((off >> 8) + (len << 5));
                        }
                        else
                        {
                            output[oidx++] = (byte)((off >> 8) + (7 << 5));
                            output[oidx++] = (byte)(len - 7);
                        }
                        output[oidx++] = (byte)off;
                        iidx += len - 1;
                        hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
                        hval = (hval << 8) | input[iidx + 2];
                        _hashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1))] = iidx;
                        iidx++;
                        hval = (hval << 8) | input[iidx + 2];
                        _hashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1))] = iidx;
                        iidx++;
                        continue;
                    }
                }
                else if (iidx == inputLength)
                    break;
                lit++;
                iidx++;
                if (lit != MaxLit) continue;
                if (oidx + 1 + MaxLit >= outputLength)
                    return 0;

                output[oidx++] = (byte)(MaxLit - 1);
                lit = -lit;
                do
                    output[oidx++] = input[iidx + lit];
                while ((++lit) != 0);
            }
            if (lit == 0) return (int)oidx;
            if (oidx + lit + 1 >= outputLength)
                return 0;
            output[oidx++] = (byte)(lit - 1);
            lit = -lit;
            do
                output[oidx++] = input[iidx + lit];
            while ((++lit) != 0);

            return (int)oidx;
        }

      3.

        /// <summary>
        /// 使用LibLZF算法解压缩数据
        /// </summary>
        /// <param name="input">参考数据进行解压缩</param>
        /// <param name="inputLength">要解压缩的数据的长度</param>
        /// <param name="output">引用包含解压缩数据的缓冲区</param>
        /// <param name="outputLength">输出缓冲区中压缩归档的大小</param>
        /// <returns>返回解压缩大小</returns>
        public int Decompress(byte[] input, int inputLength, byte[] output, int outputLength)
        {
            uint iidx = 0;
            uint oidx = 0;
            do
            {
                uint ctrl = input[iidx++];

                if (ctrl < (1 << 5))
                {
                    ctrl++;

                    if (oidx + ctrl > outputLength)
                    {
                        return 0;
                    }

                    do
                        output[oidx++] = input[iidx++];
                    while ((--ctrl) != 0);
                }
                else
                {
                    var len = ctrl >> 5;
                    var reference = (int)(oidx - ((ctrl & 0x1f) << 8) - 1);
                    if (len == 7)
                        len += input[iidx++];
                    reference -= input[iidx++];
                    if (oidx + len + 2 > outputLength)
                    {
                        return 0;
                    }
                    if (reference < 0)
                    {
                        return 0;
                    }
                    output[oidx++] = output[reference++];
                    output[oidx++] = output[reference++];
                    do
                        output[oidx++] = output[reference++];
                    while ((--len) != 0);
                }
            }
            while (iidx < inputLength);

            return (int)oidx;
        }

    以上是LZF算法的代码。

【4858.com】AppleLZF算法解析,Apple的LZF算法解析。
有关LZF算法的相关分析文书档案相比较少,可是Apple对LZF的开源,能够让大家对该算法进行1个简短的分析。…

1.数据压缩算法你用过两种?
事先知道压缩有文件类ZIP,GZIP,图片类,品质滑坡,采集样品率压缩等,当然图片也是文件的一种,类似于采集样品率压缩只适合图片类;
这个说出来后,推断大牌们会戏弄:“真low,就清楚那点,市面上有多数开源的,分分钟给你写个。。。。”。
真的市面上开源的或已融为1体近个语言开辟的sdk中有那个。。。。
粗略总括了下有如下一些:
ZLIB、GZIP、BZIP、snappy、LZO、LZF、FastLZ和QuickLZ等等
那正是说她们有怎样不相同吧?
ZLIB,GZIP选用DEFLATE压缩,那DEFLATE又是哪些啊
DEFLATE是还要利用了LZ7七算法与哈夫曼编码(Huffman
Coding)的3个无毒数据压缩算法;
jdk中对zlib压缩库提供了支持,压缩类Deflater和解压类Inflater,Deflater和Inflater都提供了native方法

    接下去看一下开源的LZF算法的贯彻源码。

private native int deflateBytes(long addr, byte[] b, int off, int len,
int flush);
private native int inflateBytes(long addr, byte[] b, int off, int len)
throws DataFormatException;
所有可以直接使用jdk提供的压缩类Deflater和解压类Inflater,代码如下:
 public static byte[] compress(byte input[]) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Deflater compressor = new Deflater(1);
        try {
            compressor.setInput(input);
            compressor.finish();
            final byte[] buf = new byte[2048];
            while (!compressor.finished()) {
                int count = compressor.deflate(buf);
                bos.write(buf, 0, count);
            }
        } finally {
            compressor.end();
        }
        return bos.toByteArray();
    }

      public static byte[] uncompress(byte[] input) throws DataFormatException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Inflater decompressor = new Inflater();
        try {
            decompressor.setInput(input);
            final byte[] buf = new byte[2048];
            while (!decompressor.finished()) {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
        } finally {
            decompressor.end();
        }
        return bos.toByteArray();
    }

     一.定义的全局字段:

能够内定算法的减弱品级,那样你能够在调整和减弱时间和出口文件大小上开展平衡。可选的等第有0(不减少),以及壹(火速缩短)到玖(慢速压缩),这里运用的是以速度为预先。
GZIP只是在deflate格式上扩展了文本头和文件尾,一样jdk也对gzip提供了辅助,分别是GZIPOutputStream和GZIPInputStream类,同样能够窥见GZIPOutputStream是继续于DeflaterOutputStream的,GZIPInputStream承袭于InflaterInputStream,并且能够在源码中开采writeHeader和writeTrailer方法:

       private readonly long[] _hashTable = new long[Hsize];

        private const uint Hlog = 14;

        private const uint Hsize = (1 << 14);

        private const uint MaxLit = (1 << 5);

        private const uint MaxOff = (1 << 13);

        private const uint MaxRef = ((1 << 8) + (1 << 3));
private void writeHeader() throws IOException {
     ......
}
private void writeTrailer(byte[] buf, int offset) throws IOException {
     ......
}

    贰.施用LibLZF算法压缩数量:

bzip二是JulianSeward开拓并依据自由软件/开源软件协议公布的数据压缩算法及程序。Seward在一九九八年一月第贰回公开揭露了bzip2
0.15版,在随之几年中这一个压缩工具稳固性获得革新并且稳步流行,Seward在三千年晚些时候发表了一.0版。
bzip二比传统的gzip的回落功用更加高,不过它的收缩速度异常慢。
jdk中并未有对bzip贰实现,不过在commons-compress中开始展览了贯彻,maven引入:

        /// <summary>
        /// 使用LibLZF算法压缩数据
        /// </summary>
        /// <param name="input">需要压缩的数据</param>
        /// <param name="inputLength">要压缩的数据的长度</param>
        /// <param name="output">引用将包含压缩数据的缓冲区</param>
        /// <param name="outputLength">压缩缓冲区的长度(应大于输入缓冲区)</param>
        /// <returns>输出缓冲区中压缩归档的大小</returns>
        public int Compress(byte[] input, int inputLength, byte[] output, int outputLength)
        {
            Array.Clear(_hashTable, 0, (int)Hsize);
            uint iidx = 0;
            uint oidx = 0;
            var hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
            var lit = 0;
            for (; ; )
            {
                if (iidx < inputLength - 2)
                {
                    hval = (hval << 8) | input[iidx + 2];
                    long hslot = ((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1));
                    var reference = _hashTable[hslot];
                    _hashTable[hslot] = iidx;
                    long off;
                    if ((off = iidx - reference - 1) < MaxOff
                        && iidx + 4 < inputLength
                        && reference > 0
                        && input[reference + 0] == input[iidx + 0]
                        && input[reference + 1] == input[iidx + 1]
                        && input[reference + 2] == input[iidx + 2]
                        )
                    {
                        uint len = 2;
                        var maxlen = (uint)inputLength - iidx - len;
                        maxlen = maxlen > MaxRef ? MaxRef : maxlen;
                        if (oidx + lit + 1 + 3 >= outputLength)
                            return 0;
                        do
                            len++;
                        while (len < maxlen && input[reference + len] == input[iidx + len]);
                        if (lit != 0)
                        {
                            output[oidx++] = (byte)(lit - 1);
                            lit = -lit;
                            do
                                output[oidx++] = input[iidx + lit];
                            while ((++lit) != 0);
                        }
                        len -= 2;
                        iidx++;
                        if (len < 7)
                        {
                            output[oidx++] = (byte)((off >> 8) + (len << 5));
                        }
                        else
                        {
                            output[oidx++] = (byte)((off >> 8) + (7 << 5));
                            output[oidx++] = (byte)(len - 7);
                        }
                        output[oidx++] = (byte)off;
                        iidx += len - 1;
                        hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
                        hval = (hval << 8) | input[iidx + 2];
                        _hashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1))] = iidx;
                        iidx++;
                        hval = (hval << 8) | input[iidx + 2];
                        _hashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1))] = iidx;
                        iidx++;
                        continue;
                    }
                }
                else if (iidx == inputLength)
                    break;
                lit++;
                iidx++;
                if (lit != MaxLit) continue;
                if (oidx + 1 + MaxLit >= outputLength)
                    return 0;

                output[oidx++] = (byte)(MaxLit - 1);
                lit = -lit;
                do
                    output[oidx++] = input[iidx + lit];
                while ((++lit) != 0);
            }
            if (lit == 0) return (int)oidx;
            if (oidx + lit + 1 >= outputLength)
                return 0;
            output[oidx++] = (byte)(lit - 1);
            lit = -lit;
            do
                output[oidx++] = input[iidx + lit];
            while ((++lit) != 0);

            return (int)oidx;
        }
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.12</version>
</dependency>

      3.

4858.com ,未完待续,,,,

        /// <summary>
        /// 使用LibLZF算法解压缩数据
        /// </summary>
        /// <param name="input">参考数据进行解压缩</param>
        /// <param name="inputLength">要解压缩的数据的长度</param>
        /// <param name="output">引用包含解压缩数据的缓冲区</param>
        /// <param name="outputLength">输出缓冲区中压缩归档的大小</param>
        /// <returns>返回解压缩大小</returns>
        public int Decompress(byte[] input, int inputLength, byte[] output, int outputLength)
        {
            uint iidx = 0;
            uint oidx = 0;
            do
            {
                uint ctrl = input[iidx++];

                if (ctrl < (1 << 5))
                {
                    ctrl++;

                    if (oidx + ctrl > outputLength)
                    {
                        return 0;
                    }

                    do
                        output[oidx++] = input[iidx++];
                    while ((--ctrl) != 0);
                }
                else
                {
                    var len = ctrl >> 5;
                    var reference = (int)(oidx - ((ctrl & 0x1f) << 8) - 1);
                    if (len == 7)
                        len += input[iidx++];
                    reference -= input[iidx++];
                    if (oidx + len + 2 > outputLength)
                    {
                        return 0;
                    }
                    if (reference < 0)
                    {
                        return 0;
                    }
                    output[oidx++] = output[reference++];
                    output[oidx++] = output[reference++];
                    do
                        output[oidx++] = output[reference++];
                    while ((--len) != 0);
                }
            }
            while (iidx < inputLength);

            return (int)oidx;
        }

    以上是LZF算法的代码。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有