CMSCompressedData.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Zlib;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  10. {
  11. /**
  12. * containing class for an CMS Compressed Data object
  13. */
  14. public class CmsCompressedData
  15. {
  16. internal ContentInfo contentInfo;
  17. public CmsCompressedData(
  18. byte[] compressedData)
  19. : this(CmsUtilities.ReadContentInfo(compressedData))
  20. {
  21. }
  22. public CmsCompressedData(
  23. Stream compressedDataStream)
  24. : this(CmsUtilities.ReadContentInfo(compressedDataStream))
  25. {
  26. }
  27. public CmsCompressedData(
  28. ContentInfo contentInfo)
  29. {
  30. this.contentInfo = contentInfo;
  31. }
  32. /**
  33. * Return the uncompressed content.
  34. *
  35. * @return the uncompressed content
  36. * @throws CmsException if there is an exception uncompressing the data.
  37. */
  38. public byte[] GetContent()
  39. {
  40. CompressedData comData = CompressedData.GetInstance(contentInfo.Content);
  41. ContentInfo content = comData.EncapContentInfo;
  42. Asn1OctetString bytes = (Asn1OctetString) content.Content;
  43. ZInputStream zIn = new ZInputStream(bytes.GetOctetStream());
  44. try
  45. {
  46. return CmsUtilities.StreamToByteArray(zIn);
  47. }
  48. catch (IOException e)
  49. {
  50. throw new CmsException("exception reading compressed stream.", e);
  51. }
  52. finally
  53. {
  54. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(zIn);
  55. }
  56. }
  57. /**
  58. * Return the uncompressed content, throwing an exception if the data size
  59. * is greater than the passed in limit. If the content is exceeded getCause()
  60. * on the CMSException will contain a StreamOverflowException
  61. *
  62. * @param limit maximum number of bytes to read
  63. * @return the content read
  64. * @throws CMSException if there is an exception uncompressing the data.
  65. */
  66. public byte[] GetContent(int limit)
  67. {
  68. CompressedData comData = CompressedData.GetInstance(contentInfo.Content);
  69. ContentInfo content = comData.EncapContentInfo;
  70. Asn1OctetString bytes = (Asn1OctetString)content.Content;
  71. ZInputStream zIn = new ZInputStream(new MemoryStream(bytes.GetOctets(), false));
  72. try
  73. {
  74. return CmsUtilities.StreamToByteArray(zIn, limit);
  75. }
  76. catch (IOException e)
  77. {
  78. throw new CmsException("exception reading compressed stream.", e);
  79. }
  80. }
  81. /**
  82. * return the ContentInfo
  83. */
  84. public ContentInfo ContentInfo
  85. {
  86. get { return contentInfo; }
  87. }
  88. /**
  89. * return the ASN.1 encoded representation of this object.
  90. */
  91. public byte[] GetEncoded()
  92. {
  93. return contentInfo.GetEncoded();
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif