Skip to content

Commit 8e4ba6e

Browse files
committed
Merge branch 'pg-synchronize-bc_csharp' into 'main'
Refactor code in pg See merge request root/bc-java!17
2 parents 4cdf535 + 058cacf commit 8e4ba6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+518
-560
lines changed

pg/src/main/java/org/bouncycastle/bcpg/ArmoredInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class ArmoredInputStream
2525
extends InputStream
2626
{
27-
/*
27+
/**
2828
* set up the decoding table.
2929
*/
3030
private static final byte[] decodingTable;

pg/src/main/java/org/bouncycastle/bcpg/BCPGInputStream.java

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
* Stream reader for PGP objects
1212
*/
1313
public class BCPGInputStream
14-
extends InputStream implements PacketTags
14+
extends InputStream
15+
implements PacketTags
1516
{
1617
/**
1718
* If the argument is a {@link BCPGInputStream}, return it.
@@ -29,19 +30,19 @@ public static BCPGInputStream wrap(InputStream in)
2930
return new BCPGInputStream(in);
3031
}
3132

32-
InputStream in;
33-
boolean next = false;
34-
int nextB;
33+
InputStream in;
34+
boolean next = false;
35+
int nextB;
3536

36-
boolean mNext = false;
37-
int mNextB;
37+
boolean mNext = false;
38+
int mNextB;
3839

3940
public BCPGInputStream(
40-
InputStream in)
41+
InputStream in)
4142
{
4243
this.in = in;
4344
}
44-
45+
4546
public int available()
4647
throws IOException
4748
{
@@ -113,9 +114,9 @@ public int read(
113114
}
114115

115116
public void readFully(
116-
byte[] buf,
117-
int off,
118-
int len)
117+
byte[] buf,
118+
int off,
119+
int len)
119120
throws IOException
120121
{
121122
if (Streams.readFully(this, buf, off, len) < len)
@@ -131,7 +132,7 @@ public byte[] readAll()
131132
}
132133

133134
public void readFully(
134-
byte[] buf)
135+
byte[] buf)
135136
throws IOException
136137
{
137138
readFully(buf, 0, buf.length);
@@ -141,7 +142,6 @@ public void readFully(
141142
* Obtains the tag of the next packet in the stream.
142143
*
143144
* @return the {@link PacketTags tag number}.
144-
*
145145
* @throws IOException if an error occurs reading the tag from the stream.
146146
*/
147147
public int nextPacketTag()
@@ -176,12 +176,13 @@ public int nextPacketTag()
176176

177177
/**
178178
* Reads the next packet from the stream.
179+
*
179180
* @throws IOException
180181
*/
181182
public Packet readPacket()
182183
throws IOException
183184
{
184-
int hdr = this.read();
185+
int hdr = this.read();
185186

186187
if (hdr < 0)
187188
{
@@ -193,36 +194,17 @@ public Packet readPacket()
193194
throw new IOException("invalid header encountered");
194195
}
195196

196-
boolean newPacket = (hdr & 0x40) != 0;
197-
int tag = 0;
198-
int bodyLen = 0;
199-
boolean partial = false;
197+
boolean newPacket = (hdr & 0x40) != 0;
198+
int tag = 0;
199+
int bodyLen = 0;
200+
boolean partial = false;
200201

201202
if (newPacket)
202203
{
203204
tag = hdr & 0x3f;
204-
205-
int l = this.read();
206-
207-
if (l < 192)
208-
{
209-
bodyLen = l;
210-
}
211-
else if (l <= 223)
212-
{
213-
int b = this.read();
214-
215-
bodyLen = ((l - 192) << 8) + (b) + 192;
216-
}
217-
else if (l == 255)
218-
{
219-
bodyLen = (this.read() << 24) | (this.read() << 16) | (this.read() << 8) | this.read();
220-
}
221-
else
222-
{
223-
partial = true;
224-
bodyLen = 1 << (l & 0x1f);
225-
}
205+
boolean[] flags = new boolean[3];
206+
bodyLen = StreamUtil.readBodyLen(this, flags);
207+
partial = flags[StreamUtil.flag_partial];
226208
}
227209
else
228210
{
@@ -236,10 +218,10 @@ else if (l == 255)
236218
bodyLen = this.read();
237219
break;
238220
case 1:
239-
bodyLen = (this.read() << 8) | this.read();
221+
bodyLen = StreamUtil.read2OctetLength(this);
240222
break;
241223
case 2:
242-
bodyLen = (this.read() << 24) | (this.read() << 16) | (this.read() << 8) | this.read();
224+
bodyLen = StreamUtil.read4OctetLength(this);
243225
break;
244226
case 3:
245227
partial = true;
@@ -249,7 +231,7 @@ else if (l == 255)
249231
}
250232
}
251233

252-
BCPGInputStream objStream;
234+
BCPGInputStream objStream;
253235

254236
if (bodyLen == 0 && partial)
255237
{
@@ -260,7 +242,7 @@ else if (l == 255)
260242
objStream = new BCPGInputStream(
261243
new BufferedInputStream(new PartialInputStream(this, partial, bodyLen)));
262244
}
263-
245+
264246
switch (tag)
265247
{
266248
case RESERVED:
@@ -314,9 +296,9 @@ else if (l == 255)
314296
}
315297

316298
/**
317-
* @deprecated use skipMarkerAndPaddingPackets
318299
* @return the tag for the next non-marker/padding packet
319300
* @throws IOException on a parsing issue.
301+
* @deprecated use skipMarkerAndPaddingPackets
320302
*/
321303
public int skipMarkerPackets()
322304
throws IOException
@@ -326,6 +308,7 @@ public int skipMarkerPackets()
326308

327309
/**
328310
* skip any marker and padding packets found in the stream.
311+
*
329312
* @return the tag for the next non-marker/padding packet
330313
* @throws IOException on a parsing issue.
331314
*/
@@ -334,7 +317,7 @@ public int skipMarkerAndPaddingPackets()
334317
{
335318
int tag;
336319
while ((tag = nextPacketTag()) == PacketTags.MARKER
337-
|| tag == PacketTags.PADDING)
320+
|| tag == PacketTags.PADDING)
338321
{
339322
readPacket();
340323
}
@@ -350,20 +333,20 @@ public void close()
350333

351334
/**
352335
* a stream that overlays our input stream, allowing the user to only read a segment of it.
353-
*
336+
* <p>
354337
* NB: dataLength will be negative if the segment length is in the upper range above 2**31.
355338
*/
356339
private static class PartialInputStream
357340
extends InputStream
358341
{
359-
private BCPGInputStream in;
360-
private boolean partial;
361-
private int dataLength;
342+
private BCPGInputStream in;
343+
private boolean partial;
344+
private int dataLength;
362345

363346
PartialInputStream(
364-
BCPGInputStream in,
365-
boolean partial,
366-
int dataLength)
347+
BCPGInputStream in,
348+
boolean partial,
349+
int dataLength)
367350
{
368351
this.in = in;
369352
this.partial = partial;
@@ -392,32 +375,13 @@ public int available()
392375
private int loadDataLength()
393376
throws IOException
394377
{
395-
int l = in.read();
396-
397-
if (l < 0)
378+
boolean[] flags = new boolean[3];
379+
dataLength = StreamUtil.readBodyLen(in, flags);
380+
if (flags[StreamUtil.flag_eof])
398381
{
399382
return -1;
400383
}
401-
402-
partial = false;
403-
if (l < 192)
404-
{
405-
dataLength = l;
406-
}
407-
else if (l <= 223)
408-
{
409-
dataLength = ((l - 192) << 8) + (in.read()) + 192;
410-
}
411-
else if (l == 255)
412-
{
413-
dataLength = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
414-
}
415-
else
416-
{
417-
partial = true;
418-
dataLength = 1 << (l & 0x1f);
419-
}
420-
384+
partial = flags[StreamUtil.flag_partial];
421385
return dataLength;
422386
}
423387

pg/src/main/java/org/bouncycastle/bcpg/BCPGOutputStream.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ private void writeHeader(
206206
else if (bodyLen <= 0xffff)
207207
{
208208
this.write(hdr | 0x01);
209-
this.write((byte)(bodyLen >> 8));
210-
this.write((byte)(bodyLen));
209+
StreamUtil.write2OctetLength(this, (int)bodyLen);
211210
}
212211
else
213212
{

pg/src/main/java/org/bouncycastle/bcpg/LiteralDataPacket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class LiteralDataPacket
4343
fileName[i] = (byte)ch;
4444
}
4545

46-
modDate = ((long)in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
46+
modDate = StreamUtil.readTime(in);
4747
if (modDate < 0)
4848
{
4949
throw new IOException("literal data truncated in header");
@@ -63,7 +63,7 @@ public int getFormat()
6363
*/
6464
public long getModificationTime()
6565
{
66-
return modDate * 1000L;
66+
return modDate;
6767
}
6868

6969
/**

pg/src/main/java/org/bouncycastle/bcpg/MPInteger.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public MPInteger(
1515
BCPGInputStream in)
1616
throws IOException
1717
{
18-
int length = (in.read() << 8) | in.read();
18+
int length = StreamUtil.read2OctetLength(in);
1919
byte[] bytes = new byte[(length + 7) / 8];
2020

2121
in.readFully(bytes);
@@ -43,11 +43,8 @@ public void encode(
4343
BCPGOutputStream out)
4444
throws IOException
4545
{
46-
int length = value.bitLength();
47-
48-
out.write(length >> 8);
49-
out.write(length);
50-
46+
StreamUtil.write2OctetLength(out, value.bitLength());
47+
5148
byte[] bytes = value.toByteArray();
5249

5350
if (bytes[0] == 0)

pg/src/main/java/org/bouncycastle/bcpg/PublicKeyPacket.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,20 @@ public class PublicKeyPacket
134134
throw new UnsupportedPacketVersionException("Unsupported Public Key Packet version encountered: " + version);
135135
}
136136

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

139139
if (version == 2 || version == VERSION_3)
140140
{
141-
validDays = (in.read() << 8) | in.read();
141+
validDays = StreamUtil.read2OctetLength(in);
142142
}
143143

144144
algorithm = (byte)in.read();
145145
long keyOctets = -1;
146-
146+
147147
if (version == LIBREPGP_5 || version == VERSION_6)
148148
{
149149
// TODO: Use keyOctets to be able to parse unknown keys
150-
keyOctets = ((long)in.read() << 24) | ((long)in.read() << 16) | ((long)in.read() << 8) | in.read();
150+
keyOctets = StreamUtil.read4OctetLength(in);
151151
}
152152

153153
parseKey(in, algorithm, keyOctets);
@@ -328,16 +328,14 @@ public byte[] getEncodedContents()
328328

329329
if (version <= VERSION_3)
330330
{
331-
pOut.write((byte)(validDays >> 8));
332-
pOut.write((byte)validDays);
331+
StreamUtil.write2OctetLength(pOut, validDays);
333332
}
334333

335334
pOut.write(algorithm);
336335

337336
if (version == VERSION_6 || version == LIBREPGP_5)
338337
{
339-
int keyOctets = key.getEncoded().length;
340-
StreamUtil.write4OctetLength(pOut, keyOctets);
338+
StreamUtil.write4OctetLength(pOut, key.getEncoded().length);
341339
}
342340

343341
pOut.writeObject((BCPGObject)key);

pg/src/main/java/org/bouncycastle/bcpg/SecretKeyPacket.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
66

7+
import org.bouncycastle.util.Arrays;
78
import org.bouncycastle.util.io.Streams;
89

910
/**
@@ -348,7 +349,7 @@ public SecretKeyPacket(
348349
this.aeadAlgorithm = aeadAlgorithm;
349350
this.s2kUsage = s2kUsage;
350351
this.s2k = s2k;
351-
this.iv = iv;
352+
this.iv = Arrays.clone(iv);
352353
this.secKeyData = secKeyData;
353354

354355
if (s2k != null && s2k.getType() == S2K.ARGON_2 && s2kUsage != USAGE_AEAD)
@@ -398,7 +399,7 @@ public int getS2KUsage()
398399
*/
399400
public byte[] getIV()
400401
{
401-
return iv;
402+
return Arrays.clone(iv);
402403
}
403404

404405
/**

0 commit comments

Comments
 (0)