/// <summary>
	    /// Construct instance with pending buffer
	    /// </summary>
	    /// <param name="pending">
	    /// Pending buffer to use
	    /// </param>>
	    public DeflaterEngine(DeflaterPending pending) 
	    {
	        this.pending = pending;
	        huffman = new DeflaterHuffman(pending);
	        adler = new Adler32();
			
	        window = new byte[2 * WSIZE];
	        head   = new short[HASH_SIZE];
	        prev   = new short[WSIZE];
			
	        // We start at index 1, to avoid an implementation deficiency, that
	        // we cannot build a repeat pattern at index 0.
	        blockStart = strstart = 1;
	    }
示例#2
0
        /// <summary>
        /// Construct instance with pending buffer
        /// </summary>
        /// <param name="pending">
        /// Pending buffer to use
        /// </param>>
        public DeflaterEngine(DeflaterPending pending)
        {
            this.pending = pending;
            huffman      = new DeflaterHuffman(pending);
            adler        = new Adler32();

            window = new byte[2 * WSIZE];
            head   = new short[HASH_SIZE];
            prev   = new short[WSIZE];

            // We start at index 1, to avoid an implementation deficiency, that
            // we cannot build a repeat pattern at index 0.
            blockStart = strstart = 1;
        }
	        public Tree(DeflaterHuffman dh, int elems, int minCodes, int maxLength) 
	        {
	            this.dh =  dh;
	            minNumCodes = minCodes;
	            this.maxLength  = maxLength;
	            freqs  = new short[elems];
	            bl_counts = new int[maxLength];
	        }
        void BuildTree(byte[] codeLengths)
        {
            var blCount  = new int[MAX_BITLEN + 1];
            var nextCode = new int[MAX_BITLEN + 1];

            for (var i = 0; i < codeLengths.Length; i++)
            {
                int bits = codeLengths[i];
                if (bits > 0)
                {
                    blCount[bits]++;
                }
            }

            var code     = 0;
            var treeSize = 512;

            for (var bits = 1; bits <= MAX_BITLEN; bits++)
            {
                nextCode[bits] = code;
                code          += blCount[bits] << (16 - bits);
                if (bits >= 10)
                {
                    /* We need an extra table for bit lengths >= 10. */
                    var start = nextCode[bits] & 0x1ff80;
                    var end   = code & 0x1ff80;
                    treeSize += (end - start) >> (16 - bits);
                }
            }

/* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
 *                      if (code != 65536)
 *                      {
 *                              throw new SharpZipBaseException("Code lengths don't add up properly.");
 *                      }
 */
            /* Now create and fill the extra tables from longest to shortest
             * bit len.  This way the sub trees will be aligned.
             */
            tree = new short[treeSize];
            var treePtr = 512;

            for (var bits = MAX_BITLEN; bits >= 10; bits--)
            {
                var end = code & 0x1ff80;
                code -= blCount[bits] << (16 - bits);
                var start = code & 0x1ff80;
                for (var i = start; i < end; i += 1 << 7)
                {
                    tree[DeflaterHuffman.BitReverse(i)] = (short)((-treePtr << 4) | bits);
                    treePtr += 1 << (bits - 9);
                }
            }

            for (var i = 0; i < codeLengths.Length; i++)
            {
                int bits = codeLengths[i];
                if (bits == 0)
                {
                    continue;
                }
                code = nextCode[bits];
                int revcode = DeflaterHuffman.BitReverse(code);
                if (bits <= 9)
                {
                    do
                    {
                        tree[revcode] = (short)((i << 4) | bits);
                        revcode      += 1 << bits;
                    } while (revcode < 512);
                }
                else
                {
                    int subTree = tree[revcode & 511];
                    var treeLen = 1 << (subTree & 15);
                    subTree = -(subTree >> 4);
                    do
                    {
                        tree[subTree | (revcode >> 9)] = (short)((i << 4) | bits);
                        revcode += 1 << bits;
                    } while (revcode < treeLen);
                }
                nextCode[bits] = code + (1 << (16 - bits));
            }
        }