summary refs log tree commit diff
path: root/Crypto/src/crypto/tls/TlsDeflateCompression.cs
blob: 146c961c7be82d8b4cf426799141c0685a890e77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.IO;

using Org.BouncyCastle.Utilities.Zlib;

namespace Org.BouncyCastle.Crypto.Tls
{
	public class TlsDeflateCompression
		: TlsCompression
	{
		protected ZStream zIn, zOut;

		public TlsDeflateCompression()
		{
			this.zIn = new ZStream();
			this.zIn.inflateInit();

			this.zOut = new ZStream();
			// TODO Allow custom setting
			this.zOut.deflateInit(JZlib.Z_DEFAULT_COMPRESSION);
		}

		public virtual Stream Compress(Stream output)
		{
			return new DeflateOutputStream(output, zOut, true);
		}

		public virtual Stream Decompress(Stream output)
		{
			return new DeflateOutputStream(output, zIn, false);
		}

		protected class DeflateOutputStream : ZOutputStream
		{
			public DeflateOutputStream(Stream output, ZStream z, bool compress)
				: base(output)
			{
				this.z = z;
				this.compress = compress;
                // TODO http://www.bolet.org/~pornin/deflate-flush.html says we should use Z_SYNC_FLUSH
				this.FlushMode = JZlib.Z_PARTIAL_FLUSH;
			}
		}
	}
}