Skip to content

Commit 4891925

Browse files
committed
Refactor some one-byte writes
1 parent f029b8a commit 4891925

File tree

6 files changed

+78
-33
lines changed

6 files changed

+78
-33
lines changed

crypto/src/bcpg/BcpgOutputStream.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33

4+
using Org.BouncyCastle.Crypto.Utilities;
45
using Org.BouncyCastle.Utilities;
56
using Org.BouncyCastle.Utilities.IO;
67

@@ -125,11 +126,18 @@ private void WriteNewPacketLength(long bodyLen)
125126
}
126127
else
127128
{
129+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
130+
Span<byte> buf = stackalloc byte[5];
131+
buf[0] = 0xFF;
132+
Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
133+
outStr.Write(buf);
134+
#else
128135
outStr.WriteByte(0xff);
129136
outStr.WriteByte((byte)(bodyLen >> 24));
130137
outStr.WriteByte((byte)(bodyLen >> 16));
131138
outStr.WriteByte((byte)(bodyLen >> 8));
132139
outStr.WriteByte((byte)bodyLen);
140+
#endif
133141
}
134142
}
135143

@@ -169,11 +177,18 @@ private void WriteHeader(PacketTag packetTag, bool oldPackets, bool partial, lon
169177
}
170178
else
171179
{
180+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
181+
Span<byte> buf = stackalloc byte[5];
182+
buf[0] = (byte)(hdr | 0x02);
183+
Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
184+
this.Write(buf);
185+
#else
172186
this.WriteByte((byte)(hdr | 0x02));
173187
this.WriteByte((byte)(bodyLen >> 24));
174188
this.WriteByte((byte)(bodyLen >> 16));
175189
this.WriteByte((byte)(bodyLen >> 8));
176190
this.WriteByte((byte)bodyLen);
191+
#endif
177192
}
178193
}
179194
}

crypto/src/bcpg/SignaturePacket.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44

55
using Org.BouncyCastle.Bcpg.Sig;
6+
using Org.BouncyCastle.Crypto.Utilities;
67
using Org.BouncyCastle.Utilities;
78
using Org.BouncyCastle.Utilities.Date;
89
using Org.BouncyCastle.Utilities.IO;
@@ -265,10 +266,7 @@ public byte[] GetSignatureTrailer()
265266

266267
byte[] trailer = new byte[5];
267268
trailer[0] = (byte)signatureType;
268-
trailer[1] = (byte)(time >> 24);
269-
trailer[2] = (byte)(time >> 16);
270-
trailer[3] = (byte)(time >> 8);
271-
trailer[4] = (byte)(time );
269+
Pack.UInt32_To_BE((uint)time, trailer, 1);
272270
return trailer;
273271
}
274272

crypto/src/bcpg/SignatureSubpacket.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.IO;
23

4+
using Org.BouncyCastle.Crypto.Utilities;
35
using Org.BouncyCastle.Utilities;
46

57
namespace Org.BouncyCastle.Bcpg
@@ -50,35 +52,31 @@ public void Encode(
5052
{
5153
int bodyLen = data.Length + 1;
5254

53-
if (isLongLength)
55+
if (isLongLength || bodyLen > 8383)
5456
{
57+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
58+
Span<byte> buf = stackalloc byte[5];
59+
buf[0] = 0xFF;
60+
Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
61+
os.Write(buf);
62+
#else
5563
os.WriteByte(0xff);
5664
os.WriteByte((byte)(bodyLen >> 24));
5765
os.WriteByte((byte)(bodyLen >> 16));
5866
os.WriteByte((byte)(bodyLen >> 8));
5967
os.WriteByte((byte)bodyLen);
68+
#endif
69+
}
70+
else if (bodyLen < 192)
71+
{
72+
os.WriteByte((byte)bodyLen);
6073
}
6174
else
6275
{
63-
if (bodyLen < 192)
64-
{
65-
os.WriteByte((byte)bodyLen);
66-
}
67-
else if (bodyLen <= 8383)
68-
{
69-
bodyLen -= 192;
70-
71-
os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
72-
os.WriteByte((byte)bodyLen);
73-
}
74-
else
75-
{
76-
os.WriteByte(0xff);
77-
os.WriteByte((byte)(bodyLen >> 24));
78-
os.WriteByte((byte)(bodyLen >> 16));
79-
os.WriteByte((byte)(bodyLen >> 8));
80-
os.WriteByte((byte)bodyLen);
81-
}
76+
bodyLen -= 192;
77+
78+
os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
79+
os.WriteByte((byte)bodyLen);
8280
}
8381

8482
if (critical)

crypto/src/bcpg/UserAttributeSubpacket.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33

4+
using Org.BouncyCastle.Crypto.Utilities;
45
using Org.BouncyCastle.Utilities;
56

67
namespace Org.BouncyCastle.Bcpg
@@ -43,23 +44,30 @@ public virtual void Encode(Stream os)
4344
{
4445
int bodyLen = data.Length + 1;
4546

46-
if (bodyLen < 192 && !longLength)
47+
if (longLength || bodyLen > 8383)
4748
{
49+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
50+
Span<byte> buf = stackalloc byte[5];
51+
buf[0] = 0xFF;
52+
Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
53+
os.Write(buf);
54+
#else
55+
os.WriteByte(0xff);
56+
os.WriteByte((byte)(bodyLen >> 24));
57+
os.WriteByte((byte)(bodyLen >> 16));
58+
os.WriteByte((byte)(bodyLen >> 8));
4859
os.WriteByte((byte)bodyLen);
60+
#endif
4961
}
50-
else if (bodyLen <= 8383 && !longLength)
62+
else if (bodyLen < 192)
5163
{
52-
bodyLen -= 192;
53-
54-
os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
5564
os.WriteByte((byte)bodyLen);
5665
}
5766
else
5867
{
59-
os.WriteByte(0xff);
60-
os.WriteByte((byte)(bodyLen >> 24));
61-
os.WriteByte((byte)(bodyLen >> 16));
62-
os.WriteByte((byte)(bodyLen >> 8));
68+
bodyLen -= 192;
69+
70+
os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
6371
os.WriteByte((byte)bodyLen);
6472
}
6573

crypto/src/crypto/util/SshBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ internal class SshBuilder
1212

1313
public void U32(uint value)
1414
{
15+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
16+
Span<byte> buf = stackalloc byte[4];
17+
Pack.UInt32_To_BE(value, buf);
18+
bos.Write(buf);
19+
#else
1520
bos.WriteByte(Convert.ToByte(value >> 24 & 0xFF));
1621
bos.WriteByte(Convert.ToByte(value >> 16 & 0xFF));
1722
bos.WriteByte(Convert.ToByte(value >> 8 & 0xFF));
1823
bos.WriteByte(Convert.ToByte(value & 0xFF));
24+
#endif
1925
}
2026

2127
public void WriteMpint(BigInteger n)

crypto/src/pqc/crypto/lms/Composer.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.IO;
23

4+
using Org.BouncyCastle.Crypto.Utilities;
35
using Org.BouncyCastle.Utilities;
46

57
namespace Org.BouncyCastle.Pqc.Crypto.Lms
@@ -23,25 +25,43 @@ public static Composer Compose()
2325

2426
public Composer U64Str(long n)
2527
{
28+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
29+
Span<byte> buf = stackalloc byte[8];
30+
Pack.UInt64_To_BE((ulong)n, buf);
31+
bos.Write(buf);
32+
#else
2633
U32Str((int)(n >> 32));
2734
U32Str((int)n);
35+
#endif
2836
return this;
2937
}
3038

3139
public Composer U32Str(int n)
3240
{
41+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
42+
Span<byte> buf = stackalloc byte[4];
43+
Pack.UInt32_To_BE((uint)n, buf);
44+
bos.Write(buf);
45+
#else
3346
bos.WriteByte((byte)(n >> 24));
3447
bos.WriteByte((byte)(n >> 16));
3548
bos.WriteByte((byte)(n >> 8));
3649
bos.WriteByte((byte)n);
50+
#endif
3751
return this;
3852
}
3953

4054
public Composer U16Str(int n)
4155
{
56+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
57+
Span<byte> buf = stackalloc byte[2];
58+
Pack.UInt16_To_BE((ushort)n, buf);
59+
bos.Write(buf);
60+
#else
4261
n &= 0xFFFF;
4362
bos.WriteByte((byte)(n >> 8));
4463
bos.WriteByte((byte)n);
64+
#endif
4565
return this;
4666
}
4767

0 commit comments

Comments
 (0)