/// <summary> /// Sets dictionary for the inflate operation /// </summary> /// <param name="z">A ZStream object</param> /// <param name="dictionary">An array of byte - dictionary</param> /// <param name="dictLength">Dictionary length</param> /// <returns>Operation result code</returns> internal int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength) { int index = 0; int length = dictLength; if (z == null || z.istate == null || z.istate.mode != InflateMode.DICT0) { return((int)ZLibResultCode.Z_STREAM_ERROR); } if (Adler32.GetAdler32Checksum(1L, dictionary, 0, dictLength) != z.adler) { return((int)ZLibResultCode.Z_DATA_ERROR); } z.adler = Adler32.GetAdler32Checksum(0, null, 0, 0); if (length >= (1 << z.istate.wbits)) { length = (1 << z.istate.wbits) - 1; index = dictLength - length; } z.istate.blocks.set_dictionary(dictionary, index, length); z.istate.mode = InflateMode.BLOCKS; return((int)ZLibResultCode.Z_OK); }
/// <summary> /// Resets this InfBlocks class instance /// </summary> internal void reset(ZStream z, long[] c) { if (c != null) { c[0] = check; } if (mode == InflateBlockMode.BTREE || mode == InflateBlockMode.DTREE) { blens = null; } if (mode == InflateBlockMode.CODES) { codes.free(z); } mode = InflateBlockMode.TYPE; BitK = 0; BitB = 0; ReadPos = WritePos = 0; if (this.needCheck) { z.adler = check = Adler32.GetAdler32Checksum(0L, null, 0, 0); } }
/// <summary> /// Read a new buffer from the current input stream, update the adler32 and total number of bytes read. All <see cref="deflate" /> input goes through this function so some applications may wish to modify it to avoid allocating a large <see cref="next_in" /> buffer and copying from it. /// </summary> /// <seealso cref="flush_pending"/> internal int read_buf(byte[] buf, int start, int size) { int len = _avail_in; if (len > size) { len = size; } if (len == 0) { return(0); } _avail_in -= len; if (_dstate.NoHeader == 0) { adler = Adler32.GetAdler32Checksum(adler, _next_in, _next_in_index, len); } Array.Copy(_next_in, _next_in_index, buf, start, len); _next_in_index += len; _total_in += len; return(len); }
/// <summary> /// copy as much as possible from the sliding Window to the output area /// </summary> internal int inflate_flush(ZStream z, int r) { int n; int p; int q; // local copies of source and destination pointers p = z.next_out_index; q = ReadPos; // compute number of bytes to copy as far as End of Window n = (int)((q <= WritePos?WritePos:End) - q); if (n > z.avail_out) { n = z.avail_out; } if (n != 0 && r == (int)ZLibResultCode.Z_BUF_ERROR) { r = (int)ZLibResultCode.Z_OK; } // update counters z.avail_out -= n; z.total_out += n; // update check information if (this.needCheck) { z.adler = check = Adler32.GetAdler32Checksum(check, Window, q, n); } // copy as far as End of Window Array.Copy(Window, q, z.next_out, p, n); p += n; q += n; // see if more to copy at beginning of Window if (q == End) { // wrap pointers q = 0; if (WritePos == End) { WritePos = 0; } // compute bytes to copy n = WritePos - q; if (n > z.avail_out) { n = z.avail_out; } if (n != 0 && r == (int)ZLibResultCode.Z_BUF_ERROR) { r = (int)ZLibResultCode.Z_OK; } // update counters z.avail_out -= n; z.total_out += n; // update check information if (this.needCheck) { z.adler = check = Adler32.GetAdler32Checksum(check, Window, q, n); } // copy Array.Copy(Window, q, z.next_out, p, n); p += n; q += n; } // update pointers z.next_out_index = p; ReadPos = q; // done return(r); }