From a137470bd31fd6ccd5034d1556b94ac4f50e672e Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Wed, 6 Mar 2024 16:08:35 +0100 Subject: [PATCH] No longer rethrow IOException as RuntimeException when reading from a PE file --- .../main/java/net/jsign/pe/DataDirectory.java | 6 +- .../src/main/java/net/jsign/pe/PEFile.java | 72 ++++++++----------- 2 files changed, 31 insertions(+), 47 deletions(-) diff --git a/jsign-core/src/main/java/net/jsign/pe/DataDirectory.java b/jsign-core/src/main/java/net/jsign/pe/DataDirectory.java index 9ec81fe1..1ccdecb6 100644 --- a/jsign-core/src/main/java/net/jsign/pe/DataDirectory.java +++ b/jsign-core/src/main/java/net/jsign/pe/DataDirectory.java @@ -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; } diff --git a/jsign-core/src/main/java/net/jsign/pe/PEFile.java b/jsign-core/src/main/java/net/jsign/pe/PEFile.java index 6af8c9f2..9cb8e2fb 100644 --- a/jsign-core/src/main/java/net/jsign/pe/PEFile.java +++ b/jsign-core/src/main/java/net/jsign/pe/PEFile.java @@ -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; } @@ -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)); } @@ -201,7 +193,7 @@ PEFormat getFormat() { * * @return the checksum of the image */ - long getCheckSum() { + long getCheckSum() throws IOException { return readDWord(peHeaderOffset, 88); } @@ -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); } /** @@ -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); } @@ -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 { @@ -309,7 +293,7 @@ synchronized void writeCertificateTable(byte[] data) throws IOException { } @Override - public synchronized List getSignatures() { + public synchronized List getSignatures() throws IOException { List signatures = new ArrayList<>(); for (CertificateTableEntry entry : getCertificateTable()) { @@ -352,7 +336,7 @@ public void setSignature(CMSSignedData signature) throws IOException { } } - private synchronized List getCertificateTable() { + private synchronized List getCertificateTable() throws IOException { List entries = new ArrayList<>(); DataDirectory certificateTable = getDataDirectory(DataDirectoryType.CERTIFICATE_TABLE);