members = fontFamilies.get(family.toLowerCase(Locale.ROOT));
+ if (members == null) {
+ return Collections.emptyList();
+ }
+ return Collections.unmodifiableList(new ArrayList<>(members));
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
/**
* Checks if a certain font is registered.
*
diff --git a/openpdf-core/src/test/java/org/openpdf/text/FontFactoryImpTest.java b/openpdf-core/src/test/java/org/openpdf/text/FontFactoryImpTest.java
new file mode 100644
index 000000000..c0d1e1783
--- /dev/null
+++ b/openpdf-core/src/test/java/org/openpdf/text/FontFactoryImpTest.java
@@ -0,0 +1,29 @@
+package org.openpdf.text;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+class FontFactoryImpTest {
+
+ /**
+ * Registering the same family/name combination several times must not add duplicate entries to the font family
+ * list, otherwise the list grows unbounded and slows down the lookup loop in {@link FontFactoryImp#getFont}.
+ */
+ @Test
+ void registerFamilyShouldNotStoreDuplicateFullNames() {
+ FontFactoryImp fontFactory = new FontFactoryImp();
+
+ fontFactory.registerFamily("my-family", "My-Font-Regular", null);
+ fontFactory.registerFamily("my-family", "My-Font-Regular", null);
+ fontFactory.registerFamily("my-family", "My-Font-Regular", null);
+
+ // A distinct member of the same family must still be added.
+ fontFactory.registerFamily("my-family", "My-Font-Bold", null);
+
+ assertThat(fontFactory.getRegisteredFamilies()).contains("my-family");
+ assertThat(fontFactory.getRegisteredFamily("my-family"))
+ .containsExactlyInAnyOrder("My-Font-Regular", "My-Font-Bold");
+ }
+
+}
\ No newline at end of file
diff --git a/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java b/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java
index 87f283dd7..18f8b1432 100644
--- a/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java
+++ b/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java
@@ -5,15 +5,37 @@
import org.junit.jupiter.api.Test;
/**
- * Tests for issue #1585: some PDF producers (e.g. mPDF) write the /P permissions entry of the
- * encryption dictionary as an unsigned 32-bit integer (4294963412) instead of the signed value
- * required by ISO 32000 (-3884). {@link PdfNumber#intValue()} used a direct double-to-int cast,
- * which clamps values above {@code Integer.MAX_VALUE} (JLS 5.1.3) instead of preserving the bit
- * pattern, so the RC4/AES key derivation used a wrong /P and threw BadPasswordException even for
- * documents with an empty user password.
+ * Tests for issue #1585: some PDF producers (e.g. mPDF) write the /P permissions entry of the encryption dictionary as
+ * an unsigned 32-bit integer (4294963412) instead of the signed value required by ISO 32000 (-3884).
+ * {@link PdfNumber#intValue()} used a direct double-to-int cast, which clamps values above {@code Integer.MAX_VALUE}
+ * (JLS 5.1.3) instead of preserving the bit pattern, so the RC4/AES key derivation used a wrong /P and threw
+ * BadPasswordException even for documents with an empty user password.
*/
class PdfNumberTest {
+ /**
+ * Some PDF producers write the encryption dictionary's {@code /P} (permissions) entry using its equivalent unsigned
+ * 32-bit decimal representation instead of the negative signed value, e.g. {@code 4294965956} instead of
+ * {@code -1340}. {@link PdfNumber#intValue()} must truncate such a value to its low-order 32 bits rather than
+ * clamping it to {@link Integer#MAX_VALUE}, otherwise the permission bits used to derive the decryption key no
+ * longer match the value the producer used, and a document with an empty user password is wrongly reported as
+ * requiring a password.
+ */
+ @Test
+ void intValueTruncatesOutOfRangeUnsignedRepresentationInsteadOfClamping() {
+ assertEquals(-1340, new PdfNumber("4294965956").intValue());
+ assertEquals(-1, new PdfNumber("4294967295").intValue());
+ assertEquals(Integer.MIN_VALUE, new PdfNumber(String.valueOf(1L << 31)).intValue());
+ }
+
+ @Test
+ void intValueRoundTripsValuesWithinIntRange() {
+ assertEquals(-1340, new PdfNumber("-1340").intValue());
+ assertEquals(0, new PdfNumber("0").intValue());
+ assertEquals(Integer.MAX_VALUE, new PdfNumber(String.valueOf(Integer.MAX_VALUE)).intValue());
+ assertEquals(Integer.MIN_VALUE, new PdfNumber(String.valueOf(Integer.MIN_VALUE)).intValue());
+ }
+
@Test
void intValueWrapsUnsignedInt32() {
// 4294963412 == 0xFFFFF0D4 == -3884 as signed int32 (the mPDF example from the issue)
diff --git a/openpdf-core/src/test/java/org/openpdf/text/pdf/encryption/DecryptUnsignedPermissionsTest.java b/openpdf-core/src/test/java/org/openpdf/text/pdf/encryption/DecryptUnsignedPermissionsTest.java
new file mode 100644
index 000000000..9e8d1aa60
--- /dev/null
+++ b/openpdf-core/src/test/java/org/openpdf/text/pdf/encryption/DecryptUnsignedPermissionsTest.java
@@ -0,0 +1,38 @@
+package org.openpdf.text.pdf.encryption;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.io.InputStream;
+import org.junit.jupiter.api.Test;
+import org.openpdf.text.pdf.PdfReader;
+
+/**
+ * Regression test for a document that has no user password but is nevertheless reported by {@link PdfReader} as
+ * requiring one, because its {@code /Encrypt} dictionary writes the {@code /P} (permissions) entry using the unsigned
+ * 32-bit decimal representation of a negative value (e.g. {@code 4294965956} instead of {@code -1340}) - a convention
+ * some PDF producers use.
+ *
+ * {@link org.openpdf.text.pdf.PdfNumber#intValue()} used a plain {@code (int)} narrowing cast on the underlying
+ * double, which clamps such an out-of-range value to {@link Integer#MAX_VALUE} instead of truncating it
+ * to its low-order 32 bits. The corrupted permission value then fed into the standard security handler's key
+ * derivation, producing a user key that did not match {@code /U}, so {@link PdfReader} threw a
+ * {@link org.openpdf.text.exceptions.BadPasswordException} even though the correct (empty) password was supplied.
+ */
+class DecryptUnsignedPermissionsTest {
+
+ @Test
+ void opensWithEmptyPasswordDespiteUnsignedPermissionsValue() throws IOException {
+ try (InputStream resource = getClass()
+ .getResourceAsStream("/permissions/empty-user-password-unsigned-p-value.pdf")) {
+ PdfReader pdfReader = new PdfReader(resource);
+ assertTrue(pdfReader.isEncrypted(), "PdfReader fails to report test file to be encrypted.");
+ assertFalse(pdfReader.isOwnerPasswordUsed(), "PdfReader fails to report limited permissions.");
+ assertEquals(1, pdfReader.getNumberOfPages(),
+ "PdfReader fails to report the correct number of pages");
+ pdfReader.close();
+ }
+ }
+}