mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathCMSCompressedData.cs
More file actions
97 lines (81 loc) · 3.23 KB
/
Copy pathCMSCompressedData.cs
File metadata and controls
97 lines (81 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Utilities.IO.Compression;
namespace Org.BouncyCastle.Cms
{
/// <summary>Containing class for a CMS CompressedData object.</summary>
public class CmsCompressedData
{
private readonly ContentInfo m_contentInfo;
private readonly CompressedData m_compressedData;
public CmsCompressedData(byte[] compressedData)
: this(CmsUtilities.ReadContentInfo(compressedData))
{
}
public CmsCompressedData(Stream compressedDataStream)
: this(CmsUtilities.ReadContentInfo(compressedDataStream))
{
}
public CmsCompressedData(ContentInfo contentInfo)
{
m_contentInfo = contentInfo ?? throw new ArgumentNullException(nameof(contentInfo));
m_compressedData = CmsUtilities.SafeGetContent(contentInfo, CompressedData.GetInstance);
}
public DerObjectIdentifier ContentType => m_contentInfo.ContentType;
public DerObjectIdentifier CompressedContentType => m_compressedData.EncapContentInfo.ContentType;
public CmsTypedStream GetContentStream() => new CmsTypedStream(CompressedContentType, Decompress());
/**
* Return the uncompressed content.
*
* @return the uncompressed content
* @throws CmsException if there is an exception uncompressing the data.
*/
public byte[] GetContent() => Decompress(zIn => CmsUtilities.StreamToByteArray(zIn));
/**
* Return the uncompressed content, throwing an exception if the data size
* is greater than the passed in limit. If the content is exceeded getCause()
* on the CMSException will contain a StreamOverflowException
*
* @param limit maximum number of bytes to read
* @return the content read
* @throws CMSException if there is an exception uncompressing the data.
*/
public byte[] GetContent(int limit) => Decompress(zIn => CmsUtilities.StreamToByteArray(zIn, limit));
public CompressedData CompressedData => m_compressedData;
/**
* return the ContentInfo
*/
public ContentInfo ContentInfo => m_contentInfo;
/**
* return the ASN.1 encoded representation of this object.
*/
public byte[] GetEncoded() => m_contentInfo.GetEncoded();
private byte[] Decompress(Func<Stream, byte[]> converter)
{
var zIn = Decompress();
try
{
using (zIn)
{
return converter(zIn);
}
}
catch (CmsException)
{
throw;
}
catch (Exception e)
{
throw new CmsException("exception reading compressed stream.", e);
}
}
private Stream Decompress()
{
ContentInfo encapContentInfo = m_compressedData.EncapContentInfo;
Asn1OctetString encapContent = CmsUtilities.SafeGetContent(encapContentInfo, Asn1OctetString.GetInstance);
return ZLib.DecompressInput(encapContent.GetOctetStream());
}
}
}