Composer
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;
using System;
using System.IO;
namespace Org.BouncyCastle.Pqc.Crypto.Lms
{
public sealed class Composer
{
private readonly MemoryStream bos = new MemoryStream();
private Composer()
{
}
public static Composer Compose()
{
return new Composer();
}
public unsafe Composer U64Str(long n)
{
Span<byte> span = new Span<byte>(stackalloc byte[8], 8);
Pack.UInt64_To_BE((ulong)n, span);
bos.Write(span);
return this;
}
public unsafe Composer U32Str(int n)
{
Span<byte> span = new Span<byte>(stackalloc byte[4], 4);
Pack.UInt32_To_BE((uint)n, span);
bos.Write(span);
return this;
}
public unsafe Composer U16Str(int n)
{
Span<byte> span = new Span<byte>(stackalloc byte[2], 2);
Pack.UInt16_To_BE((ushort)n, span);
bos.Write(span);
return this;
}
public Composer Bytes(IEncodable[] encodable)
{
for (int i = 0; i < encodable.Length; i++) {
byte[] encoded = encodable[i].GetEncoded();
bos.Write(encoded, 0, encoded.Length);
}
return this;
}
public Composer Bytes(IEncodable encodable)
{
byte[] encoded = encodable.GetEncoded();
bos.Write(encoded, 0, encoded.Length);
return this;
}
public Composer Pad(int v, int len)
{
while (len >= 0) {
bos.WriteByte((byte)v);
len--;
}
return this;
}
public Composer Bytes2(byte[][] arrays)
{
foreach (byte[] array in arrays) {
bos.Write(array, 0, array.Length);
}
return this;
}
public Composer Bytes2(byte[][] arrays, int start, int end)
{
for (int i = start; i != end; i++) {
bos.Write(arrays[i], 0, arrays[i].Length);
}
return this;
}
public Composer Bytes(byte[] array)
{
bos.Write(array, 0, array.Length);
return this;
}
public Composer Bytes(byte[] array, int start, int len)
{
bos.Write(array, start, len);
return this;
}
public byte[] Build()
{
return bos.ToArray();
}
public Composer PadUntil(int v, int requiredLen)
{
while (bos.Length < requiredLen) {
bos.WriteByte((byte)v);
}
return this;
}
public Composer Boolean(bool v)
{
bos.WriteByte(v ? ((byte)1) : ((byte)0));
return this;
}
}
}