123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- using System;
- using System.IO;
- #if !NETCF_1_0
- using System.Security.Cryptography;
- using CommonMPQ.SharpZipLib.Encryption;
- #endif
- namespace CommonMPQ.SharpZipLib.Zip.Compression.Streams
- {
-
-
-
-
-
- public class DeflaterOutputStream : Stream
- {
- #region Constructors
-
-
-
-
-
-
- public DeflaterOutputStream(Stream baseOutputStream)
- : this(baseOutputStream, new Deflater(), 512)
- {
- }
-
-
-
-
-
-
-
-
-
-
-
- public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater)
- : this(baseOutputStream, deflater, 512)
- {
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufferSize)
- {
- if ( baseOutputStream == null ) {
- throw new ArgumentNullException("baseOutputStream");
- }
- if (baseOutputStream.CanWrite == false) {
- throw new ArgumentException("Must support writing", "baseOutputStream");
- }
- if (deflater == null) {
- throw new ArgumentNullException("deflater");
- }
-
- if (bufferSize < 512) {
- throw new ArgumentOutOfRangeException("bufferSize");
- }
-
- baseOutputStream_ = baseOutputStream;
- buffer_ = new byte[bufferSize];
- deflater_ = deflater;
- }
- #endregion
-
- #region Public API
-
-
-
-
-
-
- public virtual void Finish()
- {
- deflater_.Finish();
- while (!deflater_.IsFinished) {
- int len = deflater_.Deflate(buffer_, 0, buffer_.Length);
- if (len <= 0) {
- break;
- }
- #if NETCF_1_0
- if ( keys != null ) {
- #else
- if (cryptoTransform_ != null) {
- #endif
- EncryptBlock(buffer_, 0, len);
- }
-
- baseOutputStream_.Write(buffer_, 0, len);
- }
- if (!deflater_.IsFinished) {
- throw new SharpZipBaseException("Can't deflate all input?");
- }
- baseOutputStream_.Flush();
-
- #if NETCF_1_0
- if ( keys != null ) {
- keys = null;
- }
- #else
- if (cryptoTransform_ != null) {
- #if !NET_1_1 && !NETCF_2_0
- if (cryptoTransform_ is ZipAESTransform) {
- AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode();
- }
- #endif
- cryptoTransform_.Dispose();
- cryptoTransform_ = null;
- }
- #endif
- }
-
-
-
-
-
- public bool IsStreamOwner
- {
- get { return isStreamOwner_; }
- set { isStreamOwner_ = value; }
- }
-
-
-
-
- public bool CanPatchEntries {
- get {
- return baseOutputStream_.CanSeek;
- }
- }
-
- #endregion
-
- #region Encryption
-
- string password;
-
- #if NETCF_1_0
- uint[] keys;
- #else
- ICryptoTransform cryptoTransform_;
-
-
-
- protected byte[] AESAuthCode;
- #endif
-
-
-
-
-
- public string Password {
- get {
- return password;
- }
- set {
- if ( (value != null) && (value.Length == 0) ) {
- password = null;
- } else {
- password = value;
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- protected void EncryptBlock(byte[] buffer, int offset, int length)
- {
- #if NETCF_1_0
- for (int i = offset; i < offset + length; ++i) {
- byte oldbyte = buffer[i];
- buffer[i] ^= EncryptByte();
- UpdateKeys(oldbyte);
- }
- #else
- cryptoTransform_.TransformBlock(buffer, 0, length, buffer, 0);
- #endif
- }
-
-
-
-
- protected void InitializePassword(string password)
- {
- #if NETCF_1_0
- keys = new uint[] {
- 0x12345678,
- 0x23456789,
- 0x34567890
- };
-
- byte[] rawPassword = ZipConstants.ConvertToArray(password);
-
- for (int i = 0; i < rawPassword.Length; ++i) {
- UpdateKeys((byte)rawPassword[i]);
- }
-
- #else
- PkzipClassicManaged pkManaged = new PkzipClassicManaged();
- byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
- cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
- #endif
- }
- #if !NET_1_1 && !NETCF_2_0
-
-
-
- protected void InitializeAESPassword(ZipEntry entry, string rawPassword,
- out byte[] salt, out byte[] pwdVerifier) {
- salt = new byte[entry.AESSaltLen];
-
- if (_aesRnd == null)
- _aesRnd = new RNGCryptoServiceProvider();
- _aesRnd.GetBytes(salt);
- int blockSize = entry.AESKeySize / 8;
- cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
- pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
- }
- #endif
- #if NETCF_1_0
-
-
-
-
-
-
-
- protected byte EncryptByte()
- {
- uint temp = ((keys[2] & 0xFFFF) | 2);
- return (byte)((temp * (temp ^ 1)) >> 8);
- }
-
-
-
- protected void UpdateKeys(byte ch)
- {
- keys[0] = Crc32.ComputeCrc32(keys[0], ch);
- keys[1] = keys[1] + (byte)keys[0];
- keys[1] = keys[1] * 134775813 + 1;
- keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
- }
- #endif
- #endregion
- #region Deflation Support
-
-
-
-
-
- protected void Deflate()
- {
- while (!deflater_.IsNeedingInput)
- {
- int deflateCount = deflater_.Deflate(buffer_, 0, buffer_.Length);
-
- if (deflateCount <= 0) {
- break;
- }
- #if NETCF_1_0
- if (keys != null)
- #else
- if (cryptoTransform_ != null)
- #endif
- {
- EncryptBlock(buffer_, 0, deflateCount);
- }
-
- baseOutputStream_.Write(buffer_, 0, deflateCount);
- }
-
- if (!deflater_.IsNeedingInput) {
- throw new SharpZipBaseException("DeflaterOutputStream can't deflate all input?");
- }
- }
- #endregion
-
- #region Stream Overrides
-
-
-
- public override bool CanRead
- {
- get {
- return false;
- }
- }
-
-
-
-
-
- public override bool CanSeek {
- get {
- return false;
- }
- }
-
-
-
-
- public override bool CanWrite {
- get {
- return baseOutputStream_.CanWrite;
- }
- }
-
-
-
-
- public override long Length {
- get {
- return baseOutputStream_.Length;
- }
- }
-
-
-
-
-
- public override long Position {
- get {
- return baseOutputStream_.Position;
- }
- set {
- throw new NotSupportedException("Position property not supported");
- }
- }
-
-
-
-
-
-
-
-
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException("DeflaterOutputStream Seek not supported");
- }
-
-
-
-
-
-
- public override void SetLength(long value)
- {
- throw new NotSupportedException("DeflaterOutputStream SetLength not supported");
- }
-
-
-
-
-
-
- public override int ReadByte()
- {
- throw new NotSupportedException("DeflaterOutputStream ReadByte not supported");
- }
-
-
-
-
-
-
-
-
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- throw new NotSupportedException("DeflaterOutputStream Read not supported");
- }
-
-
-
-
-
-
-
-
-
-
-
- public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
- {
- throw new NotSupportedException("DeflaterOutputStream BeginRead not currently supported");
- }
-
-
-
-
-
-
-
-
-
-
-
- public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
- {
- throw new NotSupportedException("BeginWrite is not supported");
- }
-
-
-
-
-
- public override void Flush()
- {
- deflater_.Flush();
- Deflate();
- baseOutputStream_.Flush();
- }
-
-
-
-
-
- public override void Close()
- {
- if ( !isClosed_ ) {
- isClosed_ = true;
- try {
- Finish();
- #if NETCF_1_0
- keys=null;
- #else
- if ( cryptoTransform_ != null ) {
- GetAuthCodeIfAES();
- cryptoTransform_.Dispose();
- cryptoTransform_ = null;
- }
- #endif
- }
- finally {
- if( isStreamOwner_ ) {
- baseOutputStream_.Close();
- }
- }
- }
- }
- private void GetAuthCodeIfAES() {
- #if !NET_1_1 && !NETCF_2_0
- if (cryptoTransform_ is ZipAESTransform) {
- AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode();
- }
- #endif
- }
-
-
-
-
-
-
- public override void WriteByte(byte value)
- {
- byte[] b = new byte[1];
- b[0] = value;
- Write(b, 0, 1);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- deflater_.SetInput(buffer, offset, count);
- Deflate();
- }
- #endregion
-
- #region Instance Fields
-
-
-
-
- byte[] buffer_;
-
-
-
-
- protected Deflater deflater_;
-
-
-
-
- protected Stream baseOutputStream_;
- bool isClosed_;
-
- bool isStreamOwner_ = true;
- #endregion
- #region Static Fields
- #if !NET_1_1 && !NETCF_2_0
-
- private static RNGCryptoServiceProvider _aesRnd;
- #endif
- #endregion
- }
- }
|