pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1862979 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
Date Fri, 12 Jul 2019 14:58:09 GMT
Author: tilman
Date: Fri Jul 12 14:58:09 2019
New Revision: 1862979

URL: http://svn.apache.org/viewvc?rev=1862979&view=rev
Log:
PDFBOX-4071: SonarQube fix

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1862979&r1=1862978&r2=1862979&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java Fri Jul 12
14:58:09 2019
@@ -21,6 +21,7 @@ import static org.apache.pdfbox.util.Cha
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.security.GeneralSecurityException;
 import java.security.KeyStore;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -661,7 +662,7 @@ public class COSParser extends BaseParse
                 }
                 else if (baseObj instanceof COSArray)
                 {
-                    for (COSBase cosBase : ((COSArray) baseObj))
+                    for (COSBase cosBase : (COSArray) baseObj)
                     {
                         addNewToList(toBeParsedList, cosBase, addedObjects);
                     }
@@ -696,8 +697,7 @@ public class COSParser extends BaseParse
                             else
                             {
                                 // negative offset means we have a compressed
-                                // object within object stream;
-                                // get offset of object stream
+                                // object within object stream => get offset of object
stream
                                 COSObjectKey key = new COSObjectKey((int) -fileOffset, 0);
                                 fileOffset = document.getXrefTable().get(key);
                                 if ((fileOffset == null) || (fileOffset <= 0))
@@ -1018,7 +1018,7 @@ public class COSParser extends BaseParse
         {
             return null;
         }
-        COSNumber retVal = null;
+        COSNumber retVal;
         // maybe length was given directly
         if (lengthBaseObj instanceof COSNumber)
         {
@@ -2185,17 +2185,13 @@ public class COSParser extends BaseParse
             trailer = xrefTrailerResolver.getTrailer();
             getDocument().setTrailer(trailer);
             boolean searchForObjStreamsDone = false;
-            if (!bfSearchForTrailer(trailer))
+            if (!bfSearchForTrailer(trailer) && !searchForTrailerItems(trailer))
             {
-                // search for the different parts of the trailer dictionary
-                if (!searchForTrailerItems(trailer))
-                {
-                    // root entry wasn't found, maybe it is part of an object stream
-                    bfSearchForObjStreams();
-                    searchForObjStreamsDone = true;
-                    // search again for the root entry
-                    searchForTrailerItems(trailer);
-                }
+                // root entry wasn't found, maybe it is part of an object stream
+                bfSearchForObjStreams();
+                searchForObjStreamsDone = true;
+                // search again for the root entry
+                searchForTrailerItems(trailer);
             }
             // prepare decryption if necessary
             prepareDecryption();
@@ -2208,6 +2204,13 @@ public class COSParser extends BaseParse
         return trailer;
     }
 
+    /**
+     * Search for the different parts of the trailer dictionary.
+     *
+     * @param trailer
+     * @return true if the root was found, false if not.
+     * @throws IOException
+     */
     private boolean searchForTrailerItems(COSDictionary trailer) throws IOException
     {
         boolean rootFound = false;
@@ -2857,56 +2860,58 @@ public class COSParser extends BaseParse
      * @throws InvalidPasswordException If the password is incorrect.
      * @throws IOException if something went wrong
      */
-    private void prepareDecryption() throws InvalidPasswordException, IOException
+    private void prepareDecryption() throws IOException
     {
-        if (encryption == null)
+        if (encryption != null)
         {
-            COSBase trailerEncryptItem = document.getTrailer().getItem(COSName.ENCRYPT);
-            if (trailerEncryptItem != null && !(trailerEncryptItem instanceof COSNull))
-            {
-                if (trailerEncryptItem instanceof COSObject)
-                {
-                    COSObject trailerEncryptObj = (COSObject) trailerEncryptItem;
-                    parseDictionaryRecursive(trailerEncryptObj);
-                }
-                try
-                {
-                    encryption = new PDEncryption(document.getEncryptionDictionary());
-                    DecryptionMaterial decryptionMaterial;
-                    if (keyStoreInputStream != null)
-                    {
-                        KeyStore ks = KeyStore.getInstance("PKCS12");
-                        ks.load(keyStoreInputStream, password.toCharArray());
+            return;
+        }
+        COSBase trailerEncryptItem = document.getTrailer().getItem(COSName.ENCRYPT);
+        if (trailerEncryptItem == null || trailerEncryptItem instanceof COSNull)
+        {
+            return;
+        }
 
-                        decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias,
-                                password);
-                    }
-                    else
-                    {
-                        decryptionMaterial = new StandardDecryptionMaterial(password);
-                    }
+        if (trailerEncryptItem instanceof COSObject)
+        {
+            COSObject trailerEncryptObj = (COSObject) trailerEncryptItem;
+            parseDictionaryRecursive(trailerEncryptObj);
+        }
 
-                    securityHandler = encryption.getSecurityHandler();
-                    securityHandler.prepareForDecryption(encryption, document.getDocumentID(),
-                            decryptionMaterial);
-                    accessPermission = securityHandler.getCurrentAccessPermission();
-                }
-                catch (IOException e)
-                {
-                    throw e;
-                }
-                catch (Exception e)
-                {
-                    throw new IOException("Error (" + e.getClass().getSimpleName()
-                            + ") while creating security handler for decryption", e);
-                }
-                finally
-                {
-                    if (keyStoreInputStream != null)
-                    {
-                        IOUtils.closeQuietly(keyStoreInputStream);
-                    }
-                }
+        try
+        {
+            encryption = new PDEncryption(document.getEncryptionDictionary());
+            DecryptionMaterial decryptionMaterial;
+            if (keyStoreInputStream != null)
+            {
+                KeyStore ks = KeyStore.getInstance("PKCS12");
+                ks.load(keyStoreInputStream, password.toCharArray());
+                decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias, password);
+            }
+            else
+            {
+                decryptionMaterial = new StandardDecryptionMaterial(password);
+            }
+
+            securityHandler = encryption.getSecurityHandler();
+            securityHandler.prepareForDecryption(encryption, document.getDocumentID(),
+                    decryptionMaterial);
+            accessPermission = securityHandler.getCurrentAccessPermission();
+        }
+        catch (IOException e)
+        {
+            throw e;
+        }
+        catch (GeneralSecurityException e)
+        {
+            throw new IOException("Error (" + e.getClass().getSimpleName()
+                    + ") while creating security handler for decryption", e);
+        }
+        finally
+        {
+            if (keyStoreInputStream != null)
+            {
+                IOUtils.closeQuietly(keyStoreInputStream);
             }
         }
     }



Mime
View raw message