Skip to content

Commit

Permalink
Merge branch 'pg-synchronize-bc_csharp' into 'main'
Browse files Browse the repository at this point in the history
Refactor code in pg

See merge request root/bc-java!17
  • Loading branch information
dghgit committed Jul 25, 2024
2 parents 4cdf535 + 058cacf commit 8e4ba6e
Show file tree
Hide file tree
Showing 45 changed files with 518 additions and 560 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class ArmoredInputStream
extends InputStream
{
/*
/**
* set up the decoding table.
*/
private static final byte[] decodingTable;
Expand Down
116 changes: 40 additions & 76 deletions pg/src/main/java/org/bouncycastle/bcpg/BCPGInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* Stream reader for PGP objects
*/
public class BCPGInputStream
extends InputStream implements PacketTags
extends InputStream
implements PacketTags
{
/**
* If the argument is a {@link BCPGInputStream}, return it.
Expand All @@ -29,19 +30,19 @@ public static BCPGInputStream wrap(InputStream in)
return new BCPGInputStream(in);
}

InputStream in;
boolean next = false;
int nextB;
InputStream in;
boolean next = false;
int nextB;

boolean mNext = false;
int mNextB;
boolean mNext = false;
int mNextB;

public BCPGInputStream(
InputStream in)
InputStream in)
{
this.in = in;
}

public int available()
throws IOException
{
Expand Down Expand Up @@ -113,9 +114,9 @@ public int read(
}

public void readFully(
byte[] buf,
int off,
int len)
byte[] buf,
int off,
int len)
throws IOException
{
if (Streams.readFully(this, buf, off, len) < len)
Expand All @@ -131,7 +132,7 @@ public byte[] readAll()
}

public void readFully(
byte[] buf)
byte[] buf)
throws IOException
{
readFully(buf, 0, buf.length);
Expand All @@ -141,7 +142,6 @@ public void readFully(
* Obtains the tag of the next packet in the stream.
*
* @return the {@link PacketTags tag number}.
*
* @throws IOException if an error occurs reading the tag from the stream.
*/
public int nextPacketTag()
Expand Down Expand Up @@ -176,12 +176,13 @@ public int nextPacketTag()

/**
* Reads the next packet from the stream.
*
* @throws IOException
*/
public Packet readPacket()
throws IOException
{
int hdr = this.read();
int hdr = this.read();

if (hdr < 0)
{
Expand All @@ -193,36 +194,17 @@ public Packet readPacket()
throw new IOException("invalid header encountered");
}

boolean newPacket = (hdr & 0x40) != 0;
int tag = 0;
int bodyLen = 0;
boolean partial = false;
boolean newPacket = (hdr & 0x40) != 0;
int tag = 0;
int bodyLen = 0;
boolean partial = false;

if (newPacket)
{
tag = hdr & 0x3f;

int l = this.read();

if (l < 192)
{
bodyLen = l;
}
else if (l <= 223)
{
int b = this.read();

bodyLen = ((l - 192) << 8) + (b) + 192;
}
else if (l == 255)
{
bodyLen = (this.read() << 24) | (this.read() << 16) | (this.read() << 8) | this.read();
}
else
{
partial = true;
bodyLen = 1 << (l & 0x1f);
}
boolean[] flags = new boolean[3];
bodyLen = StreamUtil.readBodyLen(this, flags);
partial = flags[StreamUtil.flag_partial];
}
else
{
Expand All @@ -236,10 +218,10 @@ else if (l == 255)
bodyLen = this.read();
break;
case 1:
bodyLen = (this.read() << 8) | this.read();
bodyLen = StreamUtil.read2OctetLength(this);
break;
case 2:
bodyLen = (this.read() << 24) | (this.read() << 16) | (this.read() << 8) | this.read();
bodyLen = StreamUtil.read4OctetLength(this);
break;
case 3:
partial = true;
Expand All @@ -249,7 +231,7 @@ else if (l == 255)
}
}

BCPGInputStream objStream;
BCPGInputStream objStream;

if (bodyLen == 0 && partial)
{
Expand All @@ -260,7 +242,7 @@ else if (l == 255)
objStream = new BCPGInputStream(
new BufferedInputStream(new PartialInputStream(this, partial, bodyLen)));
}

switch (tag)
{
case RESERVED:
Expand Down Expand Up @@ -314,9 +296,9 @@ else if (l == 255)
}

/**
* @deprecated use skipMarkerAndPaddingPackets
* @return the tag for the next non-marker/padding packet
* @throws IOException on a parsing issue.
* @deprecated use skipMarkerAndPaddingPackets
*/
public int skipMarkerPackets()
throws IOException
Expand All @@ -326,6 +308,7 @@ public int skipMarkerPackets()

/**
* skip any marker and padding packets found in the stream.
*
* @return the tag for the next non-marker/padding packet
* @throws IOException on a parsing issue.
*/
Expand All @@ -334,7 +317,7 @@ public int skipMarkerAndPaddingPackets()
{
int tag;
while ((tag = nextPacketTag()) == PacketTags.MARKER
|| tag == PacketTags.PADDING)
|| tag == PacketTags.PADDING)
{
readPacket();
}
Expand All @@ -350,20 +333,20 @@ public void close()

/**
* a stream that overlays our input stream, allowing the user to only read a segment of it.
*
* <p>
* NB: dataLength will be negative if the segment length is in the upper range above 2**31.
*/
private static class PartialInputStream
extends InputStream
{
private BCPGInputStream in;
private boolean partial;
private int dataLength;
private BCPGInputStream in;
private boolean partial;
private int dataLength;

PartialInputStream(
BCPGInputStream in,
boolean partial,
int dataLength)
BCPGInputStream in,
boolean partial,
int dataLength)
{
this.in = in;
this.partial = partial;
Expand Down Expand Up @@ -392,32 +375,13 @@ public int available()
private int loadDataLength()
throws IOException
{
int l = in.read();

if (l < 0)
boolean[] flags = new boolean[3];
dataLength = StreamUtil.readBodyLen(in, flags);
if (flags[StreamUtil.flag_eof])
{
return -1;
}

partial = false;
if (l < 192)
{
dataLength = l;
}
else if (l <= 223)
{
dataLength = ((l - 192) << 8) + (in.read()) + 192;
}
else if (l == 255)
{
dataLength = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
}
else
{
partial = true;
dataLength = 1 << (l & 0x1f);
}

partial = flags[StreamUtil.flag_partial];
return dataLength;
}

Expand Down
3 changes: 1 addition & 2 deletions pg/src/main/java/org/bouncycastle/bcpg/BCPGOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ private void writeHeader(
else if (bodyLen <= 0xffff)
{
this.write(hdr | 0x01);
this.write((byte)(bodyLen >> 8));
this.write((byte)(bodyLen));
StreamUtil.write2OctetLength(this, (int)bodyLen);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions pg/src/main/java/org/bouncycastle/bcpg/LiteralDataPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LiteralDataPacket
fileName[i] = (byte)ch;
}

modDate = ((long)in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
modDate = StreamUtil.readTime(in);
if (modDate < 0)
{
throw new IOException("literal data truncated in header");
Expand All @@ -63,7 +63,7 @@ public int getFormat()
*/
public long getModificationTime()
{
return modDate * 1000L;
return modDate;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions pg/src/main/java/org/bouncycastle/bcpg/MPInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MPInteger(
BCPGInputStream in)
throws IOException
{
int length = (in.read() << 8) | in.read();
int length = StreamUtil.read2OctetLength(in);
byte[] bytes = new byte[(length + 7) / 8];

in.readFully(bytes);
Expand Down Expand Up @@ -43,11 +43,8 @@ public void encode(
BCPGOutputStream out)
throws IOException
{
int length = value.bitLength();

out.write(length >> 8);
out.write(length);

StreamUtil.write2OctetLength(out, value.bitLength());

byte[] bytes = value.toByteArray();

if (bytes[0] == 0)
Expand Down
14 changes: 6 additions & 8 deletions pg/src/main/java/org/bouncycastle/bcpg/PublicKeyPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,20 @@ public class PublicKeyPacket
throw new UnsupportedPacketVersionException("Unsupported Public Key Packet version encountered: " + version);
}

time = ((long) in.read() << 24) | ((long) in.read() << 16) | ((long) in.read() << 8) | in.read();
time = StreamUtil.read4OctetLength(in);

if (version == 2 || version == VERSION_3)
{
validDays = (in.read() << 8) | in.read();
validDays = StreamUtil.read2OctetLength(in);
}

algorithm = (byte)in.read();
long keyOctets = -1;

if (version == LIBREPGP_5 || version == VERSION_6)
{
// TODO: Use keyOctets to be able to parse unknown keys
keyOctets = ((long)in.read() << 24) | ((long)in.read() << 16) | ((long)in.read() << 8) | in.read();
keyOctets = StreamUtil.read4OctetLength(in);
}

parseKey(in, algorithm, keyOctets);
Expand Down Expand Up @@ -328,16 +328,14 @@ public byte[] getEncodedContents()

if (version <= VERSION_3)
{
pOut.write((byte)(validDays >> 8));
pOut.write((byte)validDays);
StreamUtil.write2OctetLength(pOut, validDays);
}

pOut.write(algorithm);

if (version == VERSION_6 || version == LIBREPGP_5)
{
int keyOctets = key.getEncoded().length;
StreamUtil.write4OctetLength(pOut, keyOctets);
StreamUtil.write4OctetLength(pOut, key.getEncoded().length);
}

pOut.writeObject((BCPGObject)key);
Expand Down
5 changes: 3 additions & 2 deletions pg/src/main/java/org/bouncycastle/bcpg/SecretKeyPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.io.Streams;

/**
Expand Down Expand Up @@ -348,7 +349,7 @@ public SecretKeyPacket(
this.aeadAlgorithm = aeadAlgorithm;
this.s2kUsage = s2kUsage;
this.s2k = s2k;
this.iv = iv;
this.iv = Arrays.clone(iv);
this.secKeyData = secKeyData;

if (s2k != null && s2k.getType() == S2K.ARGON_2 && s2kUsage != USAGE_AEAD)
Expand Down Expand Up @@ -398,7 +399,7 @@ public int getS2KUsage()
*/
public byte[] getIV()
{
return iv;
return Arrays.clone(iv);
}

/**
Expand Down
Loading

0 comments on commit 8e4ba6e

Please sign in to comment.