Skip to content

Commit 6a6c0be

Browse files
author
gefeili
committed
Add StreamUtil.readBodyLen function
1 parent e89655e commit 6a6c0be

File tree

4 files changed

+118
-153
lines changed

4 files changed

+118
-153
lines changed

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

Lines changed: 38 additions & 74 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 = StreamUtil.read4OctetLength(this);
220-
}
221-
else
222-
{
223-
partial = true;
224-
bodyLen = 1 << (l & 0x1f);
225-
}
205+
boolean[] flags = new boolean[3];
206+
bodyLen = StreamUtil.readBodyLen(this, this, flags);
207+
partial = flags[2];
226208
}
227209
else
228210
{
@@ -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, in, flags);
380+
if (flags[0])
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 = StreamUtil.read4OctetLength(in);
414-
}
415-
else
416-
{
417-
partial = true;
418-
dataLength = 1 << (l & 0x1f);
419-
}
420-
384+
partial = flags[2];
421385
return dataLength;
422386
}
423387

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,17 @@ public int read()
6868
public SignatureSubpacket readPacket()
6969
throws IOException
7070
{
71-
int l = this.read();
72-
int bodyLen;
73-
74-
if (l < 0)
71+
boolean[] flags = new boolean[3];
72+
int bodyLen = StreamUtil.readBodyLen(this, in, flags);
73+
if (flags[0])
7574
{
7675
return null;
7776
}
78-
79-
boolean isLongLength = false;
80-
81-
if (l < 192)
82-
{
83-
bodyLen = l;
84-
}
85-
else if (l <= 223)
86-
{
87-
bodyLen = ((l - 192) << 8) + (in.read()) + 192;
88-
}
89-
else if (l == 255)
90-
{
91-
isLongLength = true;
92-
bodyLen = StreamUtil.read4OctetLength(in);
93-
}
94-
else
77+
else if (flags[2])
9578
{
9679
throw new IOException("unexpected length header");
9780
}
81+
boolean isLongLength = flags[1];
9882

9983
int tag = in.read();
10084

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.OutputStream;
88
import java.nio.channels.FileChannel;
99

10+
import org.bouncycastle.util.Arrays;
11+
1012
class StreamUtil
1113
{
1214
private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();
@@ -148,10 +150,39 @@ static int read4OctetLength(InputStream in)
148150

149151
/**
150152
* Note: flags is an array of three boolean values:
151-
* flags[0] indicates l is negative
152-
* */
153-
// static int readBodyLen(InputStream in, boolean[] flags){
154-
// flags =
155-
// }
153+
* flags[0] indicates l is negative, flag for eof
154+
* flags[1] indicates (is)longLength = true
155+
* flags[2] indicate partial = true
156+
*/
157+
static int readBodyLen(InputStream in, InputStream subIn, boolean[] flags)
158+
throws IOException
159+
{
160+
Arrays.fill(flags, false);
161+
int l = in.read();
162+
int bodyLen = -1;
163+
if (l < 0)
164+
{
165+
flags[0] = true;
166+
}
167+
if (l < 192)
168+
{
169+
bodyLen = l;
170+
}
171+
else if (l <= 223)
172+
{
173+
bodyLen = ((l - 192) << 8) + (subIn.read()) + 192;
174+
}
175+
else if (l == 255)
176+
{
177+
flags[1] = true;
178+
bodyLen = StreamUtil.read4OctetLength(in);
179+
}
180+
else
181+
{
182+
flags[2] = true;
183+
bodyLen = 1 << (l & 0x1f);
184+
}
185+
return bodyLen;
186+
}
156187

157188
}

0 commit comments

Comments
 (0)