TlsDeflateCompression.cs 2.3 KB

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