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 pathCMSCompressedDataParser.cs
More file actions
68 lines (62 loc) · 2.39 KB
/
Copy pathCMSCompressedDataParser.cs
File metadata and controls
68 lines (62 loc) · 2.39 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
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Utilities.IO.Compression;
namespace Org.BouncyCastle.Cms
{
/**
* Class for reading a CMS Compressed Data stream.
* <pre>
* CMSCompressedDataParser cp = new CMSCompressedDataParser(inputStream);
*
* process(cp.GetContent().GetContentStream());
* </pre>
* Note: this class does not introduce buffering - if you are processing large files you should create
* the parser with:
* <pre>
* CMSCompressedDataParser ep = new CMSCompressedDataParser(new BufferedStream(inputStream, bufSize));
* </pre>
* where bufSize is a suitably large buffer size.
* <p>
* <b>Stream handling note:</b>
* <ul>
* <li>The constructor reads only the outer CMS ContentInfo header from the
* supplied Stream. The compressed content is drained lazily by the
* caller via {@link #GetContent()} and reading from the
* returned {@link CmsTypedStream}.</li>
* <li>The supplied Stream is <b>not closed automatically</b>. Call
* {@link #Close()} on this parser (inherited from
* {@link CmsContentInfoParser}) to close the underlying Stream, or close
* it yourself.</li>
* </ul>
* </p>
*/
public class CmsCompressedDataParser
: CmsContentInfoParser
{
public CmsCompressedDataParser(byte[] compressedData)
: this(new MemoryStream(compressedData, false))
{
}
public CmsCompressedDataParser(Stream compressedData)
: base(compressedData)
{
}
public CmsTypedStream GetContent()
{
try
{
CompressedDataParser comData = new CompressedDataParser(
(Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));
ContentInfoParser content = comData.GetEncapContentInfo();
Asn1OctetStringParser bytes = (Asn1OctetStringParser)content.GetContent(Asn1Tags.OctetString);
Stream zIn = ZLib.DecompressInput(bytes.GetOctetStream());
return new CmsTypedStream(content.ContentType, zIn);
}
catch (IOException e)
{
throw new CmsException("IOException reading compressed content.", e);
}
}
}
}