summary refs log tree commit diff
path: root/crypto/src/asn1/BEROctetStringGenerator.cs
blob: de039014bc5d337bb9603097c43932a8b3578a82 (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.IO;

using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Asn1
{
	public class BerOctetStringGenerator
		: BerGenerator
	{
		public BerOctetStringGenerator(Stream outStream)
			: base(outStream)
		{
			WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
		}

		public BerOctetStringGenerator(
			Stream	outStream,
			int		tagNo,
			bool	isExplicit)
			: base(outStream, tagNo, isExplicit)
		{
			WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
		}

		public Stream GetOctetOutputStream()
		{
			return GetOctetOutputStream(new byte[1000]); // limit for CER encoding.
		}

		public Stream GetOctetOutputStream(
			int bufSize)
		{
			return bufSize < 1
				?	GetOctetOutputStream()
				:	GetOctetOutputStream(new byte[bufSize]);
		}

		public Stream GetOctetOutputStream(
			byte[] buf)
		{
			return new BufferedBerOctetStream(this, buf);
		}

		private class BufferedBerOctetStream
			: BaseOutputStream
		{
			private byte[] _buf;
			private int    _off;
			private readonly BerOctetStringGenerator _gen;
			private readonly Asn1OutputStream _derOut;

			internal BufferedBerOctetStream(
				BerOctetStringGenerator	gen,
				byte[]					buf)
			{
				_gen = gen;
				_buf = buf;
				_off = 0;
				_derOut = Asn1OutputStream.Create(_gen.Out, Asn1Encodable.Der);
			}

			public override void WriteByte(
				byte b)
			{
				_buf[_off++] = b;

				if (_off == _buf.Length)
				{
					DerOctetString.Encode(_derOut, _buf, 0, _off);
					_off = 0;
				}
			}

			public override void Write(byte[] b, int off, int len)
			{
                int bufLen = _buf.Length;
                int available = bufLen - _off;
                if (len < available)
                {
                    Array.Copy(b, off, _buf, _off, len);
                    _off += len;
                    return;
                }

                int count = 0;
                if (_off > 0)
                {
                    Array.Copy(b, off, _buf, _off, available);
                    count += available;
                    DerOctetString.Encode(_derOut, _buf, 0, bufLen);
                }

                int remaining;
                while ((remaining = len - count) >= bufLen)
                {
                    DerOctetString.Encode(_derOut, b, off + count, bufLen);
                    count += bufLen;
                }

                Array.Copy(b, off + count, _buf, 0, remaining);
                this._off = remaining;
            }

#if PORTABLE
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
				    if (_off != 0)
				    {
					    DerOctetString.Encode(_derOut, _buf, 0, _off);
				    }

				    _gen.WriteBerEnd();
                }
                base.Dispose(disposing);
            }
#else
            public override void Close()
			{
				if (_off != 0)
				{
					DerOctetString.Encode(_derOut, _buf, 0, _off);
				}

				_gen.WriteBerEnd();
				base.Close();
			}
#endif
		}
	}
}