Skip to content

Commit

Permalink
calculate and expose data length for non-partial LiteralData packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Valodim committed Feb 17, 2015
1 parent 26c232f commit 939914d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pg/src/main/java/org/spongycastle/bcpg/BCPGInputStream.java
Expand Up @@ -276,6 +276,19 @@ public void close()
in.close();
}

/**
* Return the length of the contained data packet, if available. Returns null if the
* length is not available, which is the case for a BCPGInputStream which is not inside
* a parsed Packet, and partial data packets.
*/
public Long getBodyLengthIfAvailable() {
if (in instanceof PartialInputStream) {
long length = ((PartialInputStream) in).getBodyLength();
return length == 0 ? null : length;
}
return null;
}

/**
* a stream that overlays our input stream, allowing the user to only read a segment of it.
*
Expand All @@ -286,6 +299,7 @@ private static class PartialInputStream
{
private BCPGInputStream in;
private boolean partial;
private long totalDataLength;
private int dataLength;

PartialInputStream(
Expand All @@ -295,6 +309,8 @@ private static class PartialInputStream
{
this.in = in;
this.partial = partial;
// widen negative integer range to long
this.totalDataLength = dataLength & 0x00000000ffffffffL;
this.dataLength = dataLength;
}

Expand All @@ -317,6 +333,10 @@ public int available()
}
}

private long getBodyLength() {
return partial ? 0L : totalDataLength;
}

private int loadDataLength()
throws IOException
{
Expand Down
20 changes: 20 additions & 0 deletions pg/src/main/java/org/spongycastle/bcpg/LiteralDataPacket.java
Expand Up @@ -14,6 +14,7 @@ public class LiteralDataPacket
int format;
byte[] fileName;
long modDate;
Long literalLength;

LiteralDataPacket(
BCPGInputStream in)
Expand All @@ -31,6 +32,17 @@ public class LiteralDataPacket
}

modDate = ((long)in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();

literalLength = in.getBodyLengthIfAvailable();
if (literalLength != null) {
// length of literal data is length of the packet...
// ...minus 1 byte format, 1 byte filename length
literalLength -= 2;
// ...minus length of the filename
literalLength -= l;
// ...minus two bytes timestamp
literalLength -= 4;
}
}

/**
Expand Down Expand Up @@ -64,4 +76,12 @@ public byte[] getRawFileName()
{
return Arrays.clone(fileName);
}

/**
* Return the length of the contained literal data, if available. Returns null if the
* length is not available, which is the case for partial data packets.
*/
public Long getDataLengthIfAvailable() {
return literalLength;
}
}
9 changes: 9 additions & 0 deletions pg/src/main/java/org/spongycastle/openpgp/PGPLiteralData.java
Expand Up @@ -71,6 +71,15 @@ public Date getModificationTime()
return new Date(data.getModificationTime());
}

/**
* Return the length of the contained literal data, if available. Returns null if the
* length is not available, which is the case for partial data packets.
*/
public Long getDataLengthIfAvailable()
{
return data.getDataLengthIfAvailable();
}

/**
* Return the raw input stream for the data packet.
*/
Expand Down

0 comments on commit 939914d

Please sign in to comment.