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.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Zlib;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  10. {
  11. /**
  12. * Class for reading a CMS Compressed Data stream.
  13. * <pre>
  14. * CMSCompressedDataParser cp = new CMSCompressedDataParser(inputStream);
  15. *
  16. * process(cp.GetContent().GetContentStream());
  17. * </pre>
  18. * Note: this class does not introduce buffering - if you are processing large files you should create
  19. * the parser with:
  20. * <pre>
  21. * CMSCompressedDataParser ep = new CMSCompressedDataParser(new BufferedInputStream(inputStream, bufSize));
  22. * </pre>
  23. * where bufSize is a suitably large buffer size.
  24. */
  25. public class CmsCompressedDataParser
  26. : CmsContentInfoParser
  27. {
  28. public CmsCompressedDataParser(
  29. byte[] compressedData)
  30. : this(new MemoryStream(compressedData, false))
  31. {
  32. }
  33. public CmsCompressedDataParser(
  34. Stream compressedData)
  35. : base(compressedData)
  36. {
  37. }
  38. public CmsTypedStream GetContent()
  39. {
  40. try
  41. {
  42. CompressedDataParser comData = new CompressedDataParser((Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));
  43. ContentInfoParser content = comData.GetEncapContentInfo();
  44. Asn1OctetStringParser bytes = (Asn1OctetStringParser)content.GetContent(Asn1Tags.OctetString);
  45. return new CmsTypedStream(content.ContentType.ToString(), new ZInputStream(bytes.GetOctetStream()));
  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