TlsDeflateCompression.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.IO;
  4. using Org.BouncyCastle.Utilities.Zlib;
  5. namespace Org.BouncyCastle.Crypto.Tls
  6. {
  7. public class TlsDeflateCompression : TlsCompression
  8. {
  9. public const int LEVEL_NONE = JZlib.Z_NO_COMPRESSION;
  10. public const int LEVEL_FASTEST = JZlib.Z_BEST_SPEED;
  11. public const int LEVEL_SMALLEST = JZlib.Z_BEST_COMPRESSION;
  12. public const int LEVEL_DEFAULT = JZlib.Z_DEFAULT_COMPRESSION;
  13. protected readonly ZStream zIn, zOut;
  14. public TlsDeflateCompression()
  15. : this(LEVEL_DEFAULT)
  16. {
  17. }
  18. public TlsDeflateCompression(int level)
  19. {
  20. this.zIn = new ZStream();
  21. this.zIn.inflateInit();
  22. this.zOut = new ZStream();
  23. this.zOut.deflateInit(level);
  24. }
  25. public virtual Stream Compress(Stream output)
  26. {
  27. return new DeflateOutputStream(output, zOut, true);
  28. }
  29. public virtual Stream Decompress(Stream output)
  30. {
  31. return new DeflateOutputStream(output, zIn, false);
  32. }
  33. protected class DeflateOutputStream : ZOutputStream
  34. {
  35. public DeflateOutputStream(Stream output, ZStream z, bool compress)
  36. : base(output, z)
  37. {
  38. this.compress = compress;
  39. /*
  40. * See discussion at http://www.bolet.org/~pornin/deflate-flush.html .
  41. */
  42. this.FlushMode = JZlib.Z_SYNC_FLUSH;
  43. }
  44. public override void Flush()
  45. {
  46. /*
  47. * TODO The inflateSyncPoint doesn't appear to work the way I hoped at the moment.
  48. * In any case, we may like to accept PARTIAL_FLUSH input, not just SYNC_FLUSH.
  49. * It's not clear how to check this in the Inflater.
  50. */
  51. //if (!this.compress && (z == null || z.istate == null || z.istate.inflateSyncPoint(z) <= 0))
  52. //{
  53. // throw new TlsFatalAlert(AlertDescription.decompression_failure);
  54. //}
  55. }
  56. }
  57. }
  58. }
  59. #endif