CMSCompressedDataParser.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Compression;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  9. {
  10. /**
  11. * Class for reading a CMS Compressed Data stream.
  12. * <pre>
  13. * CMSCompressedDataParser cp = new CMSCompressedDataParser(inputStream);
  14. *
  15. * process(cp.GetContent().GetContentStream());
  16. * </pre>
  17. * Note: this class does not introduce buffering - if you are processing large files you should create
  18. * the parser with:
  19. * <pre>
  20. * CMSCompressedDataParser ep = new CMSCompressedDataParser(new BufferedInputStream(inputStream, bufSize));
  21. * </pre>
  22. * where bufSize is a suitably large buffer size.
  23. */
  24. public class CmsCompressedDataParser
  25. : CmsContentInfoParser
  26. {
  27. public CmsCompressedDataParser(
  28. byte[] compressedData)
  29. : this(new MemoryStream(compressedData, false))
  30. {
  31. }
  32. public CmsCompressedDataParser(
  33. Stream compressedData)
  34. : base(compressedData)
  35. {
  36. }
  37. public CmsTypedStream GetContent()
  38. {
  39. try
  40. {
  41. CompressedDataParser comData = new CompressedDataParser((Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));
  42. ContentInfoParser content = comData.GetEncapContentInfo();
  43. Asn1OctetStringParser bytes = (Asn1OctetStringParser)content.GetContent(Asn1Tags.OctetString);
  44. Stream zIn = ZLib.DecompressInput(bytes.GetOctetStream());
  45. return new CmsTypedStream(content.ContentType.ToString(), zIn);
  46. }
  47. catch (IOException e)
  48. {
  49. throw new CmsException("IOException reading compressed content.", e);
  50. }
  51. }
  52. }
  53. }
  54. #pragma warning restore
  55. #endif