Skip to content

Commit

Permalink
No longer rethrow IOException as RuntimeException when reading from a…
Browse files Browse the repository at this point in the history
… PE file
  • Loading branch information
ebourg committed Mar 6, 2024
1 parent 7c54a68 commit a137470
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 47 deletions.
6 changes: 3 additions & 3 deletions jsign-core/src/main/java/net/jsign/pe/DataDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class DataDirectory {
this.index = index;
}

public long getVirtualAddress() {
public long getVirtualAddress() throws IOException {
return peFile.readDWord(peFile.getDataDirectoryOffset(), index * 8);
}

public int getSize() {
public int getSize() throws IOException {
return (int) peFile.readDWord(peFile.getDataDirectoryOffset(), index * 8 + 4);
}

public boolean exists() {
public boolean exists() throws IOException {
return getVirtualAddress() != 0 && getSize() != 0;
}

Expand Down
72 changes: 28 additions & 44 deletions jsign-core/src/main/java/net/jsign/pe/PEFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,33 +150,25 @@ public synchronized void close() throws IOException {
channel.close();
}

synchronized int read(byte[] buffer, long base, int offset) {
try {
channel.position(base + offset);
return channel.read(ByteBuffer.wrap(buffer));
} catch (IOException e) {
throw new RuntimeException(e);
}
synchronized int read(byte[] buffer, long base, int offset) throws IOException {
channel.position(base + offset);
return channel.read(ByteBuffer.wrap(buffer));
}

private void read(long base, int offset, int length) {
try {
valueBuffer.limit(length);
valueBuffer.clear();
channel.position(base + offset);
channel.read(valueBuffer);
valueBuffer.rewind();
} catch (IOException e) {
throw new RuntimeException(e);
}
private void read(long base, int offset, int length) throws IOException {
valueBuffer.limit(length);
valueBuffer.clear();
channel.position(base + offset);
channel.read(valueBuffer);
valueBuffer.rewind();
}

synchronized int readWord(long base, int offset) {
synchronized int readWord(long base, int offset) throws IOException {
read(base, offset, 2);
return valueBuffer.getShort() & 0xFFFF;
}

synchronized long readDWord(long base, int offset) {
synchronized long readDWord(long base, int offset) throws IOException {
read(base, offset, 4);
return valueBuffer.getInt() & 0xFFFFFFFFL;
}
Expand All @@ -192,7 +184,7 @@ synchronized void write(long base, ByteBuffer data) throws IOException {
}
}

PEFormat getFormat() {
PEFormat getFormat() throws IOException {
return PEFormat.valueOf(readWord(peHeaderOffset, 24));
}

Expand All @@ -201,7 +193,7 @@ PEFormat getFormat() {
*
* @return the checksum of the image
*/
long getCheckSum() {
long getCheckSum() throws IOException {
return readDWord(peHeaderOffset, 88);
}

Expand All @@ -211,36 +203,28 @@ long getCheckSum() {
*
* @return the checksum of the image
*/
synchronized long computeChecksum() {
synchronized long computeChecksum() throws IOException {
PEImageChecksum checksum = new PEImageChecksum(peHeaderOffset + 88);

ByteBuffer b = ByteBuffer.allocate(64 * 1024);

try {
channel.position(0);

int len;
while ((len = channel.read(b)) > 0) {
b.flip();
checksum.update(b.array(), 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);

channel.position(0);

int len;
while ((len = channel.read(b)) > 0) {
b.flip();
checksum.update(b.array(), 0, len);
}

return checksum.getValue();
}

synchronized void updateChecksum() {
synchronized void updateChecksum() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt((int) computeChecksum());
buffer.flip();

try {
write(peHeaderOffset + 88, buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
write(peHeaderOffset + 88, buffer);
}

/**
Expand All @@ -249,11 +233,11 @@ synchronized void updateChecksum() {
*
* @return the number of data-directory entries
*/
int getNumberOfRvaAndSizes() {
int getNumberOfRvaAndSizes() throws IOException {
return (int) readDWord(peHeaderOffset, PEFormat.PE32.equals(getFormat()) ? 116 : 132);
}

int getDataDirectoryOffset() {
int getDataDirectoryOffset() throws IOException {
return (int) peHeaderOffset + (PEFormat.PE32.equals(getFormat()) ? 120 : 136);
}

Expand All @@ -263,7 +247,7 @@ int getDataDirectoryOffset() {
* @param type the type of data directory
* @return the data directory of the specified type
*/
DataDirectory getDataDirectory(DataDirectoryType type) {
DataDirectory getDataDirectory(DataDirectoryType type) throws IOException {
if (type.ordinal() >= getNumberOfRvaAndSizes()) {
return null;
} else {
Expand Down Expand Up @@ -309,7 +293,7 @@ synchronized void writeCertificateTable(byte[] data) throws IOException {
}

@Override
public synchronized List<CMSSignedData> getSignatures() {
public synchronized List<CMSSignedData> getSignatures() throws IOException {
List<CMSSignedData> signatures = new ArrayList<>();

for (CertificateTableEntry entry : getCertificateTable()) {
Expand Down Expand Up @@ -352,7 +336,7 @@ public void setSignature(CMSSignedData signature) throws IOException {
}
}

private synchronized List<CertificateTableEntry> getCertificateTable() {
private synchronized List<CertificateTableEntry> getCertificateTable() throws IOException {
List<CertificateTableEntry> entries = new ArrayList<>();
DataDirectory certificateTable = getDataDirectory(DataDirectoryType.CERTIFICATE_TABLE);

Expand Down

0 comments on commit a137470

Please sign in to comment.