123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- using System;
- namespace CommonMPQ.SharpZipLib.Zip.Compression.Streams
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class StreamManipulator
- {
- #region Constructors
-
-
-
- public StreamManipulator()
- {
- }
- #endregion
-
-
-
-
-
-
-
-
-
- public int PeekBits(int bitCount)
- {
- if (bitsInBuffer_ < bitCount) {
- if (windowStart_ == windowEnd_) {
- return -1;
- }
- int tmp1 = window_[windowStart_++] & 0xff;
- int tmp2 = window_[windowStart_++] & 0xff;
- buffer_ |= (uint)((tmp1 | tmp2 << 8) << bitsInBuffer_);
- bitsInBuffer_ += 16;
- }
- return (int)(buffer_ & ((1 << bitCount) - 1));
- }
-
-
-
-
-
-
-
- public void DropBits(int bitCount)
- {
- buffer_ >>= bitCount;
- bitsInBuffer_ -= bitCount;
- }
-
-
-
-
-
-
-
-
-
- public int GetBits(int bitCount)
- {
- int bits = PeekBits(bitCount);
- if (bits >= 0) {
- DropBits(bitCount);
- }
- return bits;
- }
-
-
-
-
-
-
-
-
- public int AvailableBits {
- get {
- return bitsInBuffer_;
- }
- }
-
-
-
-
-
-
-
- public int AvailableBytes {
- get {
- return windowEnd_ - windowStart_ + (bitsInBuffer_ >> 3);
- }
- }
-
-
-
-
- public void SkipToByteBoundary()
- {
- buffer_ >>= (bitsInBuffer_ & 7);
- bitsInBuffer_ &= ~7;
- }
-
-
-
- public bool IsNeedingInput {
- get {
- return windowStart_ == windowEnd_;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int CopyBytes(byte[] output, int offset, int length)
- {
- if (length < 0) {
- throw new ArgumentOutOfRangeException("length");
- }
- if ((bitsInBuffer_ & 7) != 0) {
-
- throw new InvalidOperationException("Bit buffer is not byte aligned!");
- }
-
- int count = 0;
- while ((bitsInBuffer_ > 0) && (length > 0)) {
- output[offset++] = (byte) buffer_;
- buffer_ >>= 8;
- bitsInBuffer_ -= 8;
- length--;
- count++;
- }
-
- if (length == 0) {
- return count;
- }
-
- int avail = windowEnd_ - windowStart_;
- if (length > avail) {
- length = avail;
- }
- System.Array.Copy(window_, windowStart_, output, offset, length);
- windowStart_ += length;
-
- if (((windowStart_ - windowEnd_) & 1) != 0) {
-
- buffer_ = (uint)(window_[windowStart_++] & 0xff);
- bitsInBuffer_ = 8;
- }
- return count + length;
- }
-
-
-
-
- public void Reset()
- {
- buffer_ = 0;
- windowStart_ = windowEnd_ = bitsInBuffer_ = 0;
- }
-
-
-
-
-
-
-
- public void SetInput(byte[] buffer, int offset, int count)
- {
- if ( buffer == null ) {
- throw new ArgumentNullException("buffer");
- }
- if ( offset < 0 ) {
- #if NETCF_1_0
- throw new ArgumentOutOfRangeException("offset");
- #else
- throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
- #endif
- }
- if ( count < 0 ) {
- #if NETCF_1_0
- throw new ArgumentOutOfRangeException("count");
- #else
- throw new ArgumentOutOfRangeException("count", "Cannot be negative");
- #endif
- }
- if (windowStart_ < windowEnd_) {
- throw new InvalidOperationException("Old input was not completely processed");
- }
-
- int end = offset + count;
-
-
-
- if ((offset > end) || (end > buffer.Length) ) {
- throw new ArgumentOutOfRangeException("count");
- }
-
- if ((count & 1) != 0) {
-
- buffer_ |= (uint)((buffer[offset++] & 0xff) << bitsInBuffer_);
- bitsInBuffer_ += 8;
- }
-
- window_ = buffer;
- windowStart_ = offset;
- windowEnd_ = end;
- }
- #region Instance Fields
- private byte[] window_;
- private int windowStart_;
- private int windowEnd_;
- private uint buffer_;
- private int bitsInBuffer_;
- #endregion
- }
- }
|