CMSCompressedData.cs 2.9 KB

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