You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2010/06/09 18:59:04 UTC

svn commit: r953078 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: cos/COSName.java pdmodel/encryption/PDCryptFilterDictionary.java pdmodel/encryption/PDEncryptionDictionary.java

Author: lehmi
Date: Wed Jun  9 16:59:04 2010
New Revision: 953078

URL: http://svn.apache.org/viewvc?rev=953078&view=rev
Log:
PDFBOX-730: added an implementation of the crypt filter dictionary.

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDCryptFilterDictionary.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDEncryptionDictionary.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=953078&r1=953077&r2=953078&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Wed Jun  9 16:59:04 2010
@@ -198,6 +198,14 @@ public final class COSName extends COSBa
     */
     public static final COSName CCITTFAX_DECODE_ABBREVIATION = new COSName( "CCF" );
     /**
+     * A common COSName value.
+     */
+    public static final COSName CF = new COSName( "CF" );
+    /**
+     * A common COSName value.
+     */
+    public static final COSName CFM = new COSName( "CFM" );
+    /**
     * A common COSName value.
     */
     public static final COSName CHAR_PROCS = new COSName( "CharProcs" );
@@ -706,6 +714,10 @@ public final class COSName extends COSBa
     /**
      * A common COSName value.
      */
+    public static final COSName RECIPIENTS = new COSName( "Recipients" );
+    /**
+     * A common COSName value.
+     */
     public static final COSName REGISTRY = new COSName( "Registry" );
     /**
     * A common COSName value.
@@ -765,11 +777,27 @@ public final class COSName extends COSBa
     * A common COSName value.
     */
     public static final COSName STANDARD_ENCODING = new COSName( "StandardEncoding" );
-
+    /**
+     * A common COSName value.
+     */
+    public static final COSName STD_CF = new COSName( "StdCF" );
+    /**
+     * A common COSName value.
+     */
+    public static final COSName STM_F = new COSName( "StmF" );
+    /**
+     * A common COSName value.
+     */
+    public static final COSName STR_F = new COSName( "StrF" );
+  
     /** "StructTreeRoot" */
     public static final COSName STRUCT_TREE_ROOT = new COSName("StructTreeRoot");
 
     /**
+     * A common COSName value.
+     */
+    public static final COSName SUB_FILTER = new COSName( "SubFilter" );
+    /**
     * A common COSName value.
     */
     public static final COSName SUBTYPE = new COSName( "Subtype" );
@@ -815,6 +843,10 @@ public final class COSName extends COSBa
     */
     public static final COSName TYPE3 = new COSName(  "Type3" );
 
+    /**
+     * A common COSName value.
+     */
+    public static final COSName U = new COSName( "U" );
     /** "URI" */
     public static final COSName URI = new COSName("URI");
 

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDCryptFilterDictionary.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDCryptFilterDictionary.java?rev=953078&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDCryptFilterDictionary.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDCryptFilterDictionary.java Wed Jun  9 16:59:04 2010
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.pdmodel.encryption;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+
+/**
+ * This class is a specialized view of the crypt filter dictionary of a PDF document.
+ * It contains a low level dictionary (COSDictionary) and provides the methods to
+ * manage its fields.
+ *
+ *
+ * @version $Revision: 1.0 $
+ */
+public class PDCryptFilterDictionary
+{
+
+    /**
+     * COS crypt filter dictionary.
+     */
+    protected COSDictionary cryptFilterDictionary = null;
+
+    /**
+     * creates a new empty crypt filter dictionary.
+     */
+    public PDCryptFilterDictionary()
+    {
+        cryptFilterDictionary = new COSDictionary();
+    }
+
+    /**
+     * creates a new crypt filter dictionary from the low level dictionary provided.
+     * @param d the low level dictionary that will be managed by the newly created object
+     */
+    public PDCryptFilterDictionary(COSDictionary d)
+    {
+        cryptFilterDictionary = d;
+    }
+
+    /**
+     * This will get the dictionary associated with this crypt filter dictionary.
+     *
+     * @return The COS dictionary that this object wraps.
+     */
+    public COSDictionary getCOSDictionary()
+    {
+        return cryptFilterDictionary;
+    }
+
+    /**
+     * This will set the number of bits to use for the crypt filter algorithm.
+     *
+     * @param length The new key length.
+     */
+    public void setLength(int length)
+    {
+        cryptFilterDictionary.setInt(COSName.LENGTH, length);
+    }
+
+    /**
+     * This will return the Length entry of the crypt filter dictionary.<br /><br />
+     * The length in <b>bits</b> for the crypt filter algorithm. This will return a multiple of 8.
+     *
+     * @return The length in bits for the encryption algorithm
+     */
+    public int getLength()
+    {
+        return cryptFilterDictionary.getInt( COSName.LENGTH, 40 );
+    }
+
+     /**
+     * This will set the crypt filter method. 
+     * Allowed values are: NONE, V2, AESV2
+     *
+     * @param cfm name of the crypt filter method.
+     *
+     * @throws IOException If there is an error setting the data.
+     */
+    public void setCryptFilterMethod(COSName cfm) throws IOException
+    {
+        cryptFilterDictionary.setItem( COSName.CFM, cfm );
+    }
+
+    /**
+     * This will return the crypt filter method. 
+     * Allowed values are: NONE, V2, AESV2
+     *
+     * @return the name of the crypt filter method.
+     *
+     * @throws IOException If there is an error accessing the data.
+     */
+    public COSName getCryptFilterMethod() throws IOException
+    {
+        return (COSName)cryptFilterDictionary.getDictionaryObject( COSName.CFM );
+    }
+
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDEncryptionDictionary.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDEncryptionDictionary.java?rev=953078&r1=953077&r2=953078&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDEncryptionDictionary.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PDEncryptionDictionary.java Wed Jun  9 16:59:04 2010
@@ -128,13 +128,23 @@ public class PDEncryptionDictionary
     }
 
     /**
+     * Get the name of the subfilter.
+     *
+     * @return The subfilter name contained in this encryption dictionary.
+     */
+    public String getSubFilter()
+    {
+        return encryptionDictionary.getNameAsString( COSName.SUB_FILTER );
+    }
+
+    /**
      * Set the subfilter entry of the encryption dictionary.
      *
      * @param subfilter The value of the subfilter field.
      */
     public void setSubFilter(String subfilter)
     {
-        encryptionDictionary.setName( "SubFilter", subfilter );
+        encryptionDictionary.setName( COSName.SUB_FILTER, subfilter );
     }
 
     /**
@@ -147,7 +157,7 @@ public class PDEncryptionDictionary
      */
     public void setVersion(int version)
     {
-        encryptionDictionary.setInt( "V", version );
+        encryptionDictionary.setInt( COSName.V, version );
     }
 
     /**
@@ -158,7 +168,7 @@ public class PDEncryptionDictionary
      */
     public int getVersion()
     {
-        return encryptionDictionary.getInt( "V", 0 );
+        return encryptionDictionary.getInt( COSName.V, 0 );
     }
 
     /**
@@ -168,7 +178,7 @@ public class PDEncryptionDictionary
      */
     public void setLength(int length)
     {
-        encryptionDictionary.setInt("Length", length);
+        encryptionDictionary.setInt(COSName.LENGTH, length);
     }
 
     /**
@@ -179,7 +189,7 @@ public class PDEncryptionDictionary
      */
     public int getLength()
     {
-        return encryptionDictionary.getInt( "Length", 40 );
+        return encryptionDictionary.getInt( COSName.LENGTH, 40 );
     }
 
     /**
@@ -193,7 +203,7 @@ public class PDEncryptionDictionary
      */
     public void setRevision(int revision)
     {
-        encryptionDictionary.setInt( "R", revision );
+        encryptionDictionary.setInt( COSName.R, revision );
     }
 
     /**
@@ -204,7 +214,7 @@ public class PDEncryptionDictionary
      */
     public int getRevision()
     {
-        return encryptionDictionary.getInt( "R", DEFAULT_VERSION );
+        return encryptionDictionary.getInt( COSName.R, DEFAULT_VERSION );
     }
 
      /**
@@ -218,7 +228,7 @@ public class PDEncryptionDictionary
     {
         COSString owner = new COSString();
         owner.append( o );
-        encryptionDictionary.setItem( COSName.getPDFName( "O" ), owner );
+        encryptionDictionary.setItem( COSName.O, owner );
     }
 
     /**
@@ -231,7 +241,7 @@ public class PDEncryptionDictionary
     public byte[] getOwnerKey() throws IOException
     {
         byte[] o = null;
-        COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "O" ) );
+        COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.O );
         if( owner != null )
         {
             o = owner.getBytes();
@@ -250,7 +260,7 @@ public class PDEncryptionDictionary
     {
         COSString user = new COSString();
         user.append( u );
-        encryptionDictionary.setItem( COSName.getPDFName( "U" ), user );
+        encryptionDictionary.setItem( COSName.U, user );
     }
 
     /**
@@ -263,7 +273,7 @@ public class PDEncryptionDictionary
     public byte[] getUserKey() throws IOException
     {
         byte[] u = null;
-        COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "U" ) );
+        COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.U );
         if( user != null )
         {
             u = user.getBytes();
@@ -278,7 +288,7 @@ public class PDEncryptionDictionary
      */
     public void setPermissions(int permissions)
     {
-        encryptionDictionary.setInt( "P", permissions );
+        encryptionDictionary.setInt( COSName.P, permissions );
     }
 
     /**
@@ -288,7 +298,7 @@ public class PDEncryptionDictionary
      */
     public int getPermissions()
     {
-        return encryptionDictionary.getInt( "P", 0 );
+        return encryptionDictionary.getInt( COSName.P, 0 );
     }
 
     /**
@@ -307,7 +317,7 @@ public class PDEncryptionDictionary
             recip.setForceLiteralForm(true);
             array.add(recip);
         }
-        encryptionDictionary.setItem(COSName.getPDFName("Recipients"), array);
+        encryptionDictionary.setItem(COSName.RECIPIENTS, array);
     }
 
     /**
@@ -317,7 +327,7 @@ public class PDEncryptionDictionary
      */
     public int getRecipientsLength()
     {
-        COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients"));
+        COSArray array = (COSArray)encryptionDictionary.getItem(COSName.RECIPIENTS);
         return array.size();
     }
 
@@ -330,7 +340,73 @@ public class PDEncryptionDictionary
      */
     public COSString getRecipientStringAt(int i)
     {
-        COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients"));
+        COSArray array = (COSArray)encryptionDictionary.getItem(COSName.RECIPIENTS);
         return (COSString)array.get(i);
     }
+    
+    /**
+     * Returns the standard crypt filter.
+     * 
+     * @return the standard crypt filter if available.
+     */
+    public PDCryptFilterDictionary getStdCryptFilterDictionary() 
+    {
+        return getCryptFilterDictionary(COSName.STD_CF);
+    }
+
+    /**
+     * Returns the crypt filter with the given name.
+     * 
+     * @param cryptFilterName the name of the crypt filter
+     * 
+     * @return the crypt filter with the given name if available
+     */
+    public PDCryptFilterDictionary getCryptFilterDictionary(COSName cryptFilterName) 
+    {
+        COSDictionary cryptFilterDictionary = (COSDictionary)encryptionDictionary.getDictionaryObject( COSName.CF );
+        if (cryptFilterDictionary != null)
+        {
+            COSDictionary stdCryptFilterDictionary = (COSDictionary)cryptFilterDictionary.getDictionaryObject(cryptFilterName);
+            if (stdCryptFilterDictionary != null)
+            {
+                return new PDCryptFilterDictionary(stdCryptFilterDictionary);
+            }
+        }
+        return null;
+    }
+    
+    /**
+     * Returns the name of the filter which is used for de/encrypting streams.
+     * Default value is "Identity".
+     * 
+     * @return the name of the filter
+     */
+    public COSName getStreamFilterName() 
+    {
+        COSName stmF = (COSName)encryptionDictionary.getDictionaryObject( COSName.STM_F );
+        if (stmF == null)
+        {
+            stmF = COSName.IDENTITY;
+        }
+        return stmF;
+    }
+
+    /**
+     * Returns the name of the filter which is used for de/encrypting strings.
+     * Default value is "Identity".
+     * 
+     * @return the name of the filter
+     */
+    public COSName getStringFilterName() 
+    {
+        COSName strF = (COSName)encryptionDictionary.getDictionaryObject( COSName.STR_F );
+        if (strF == null)
+        {
+            strF = COSName.IDENTITY;
+        }
+        return strF;
+    }
+
 }
+
+    
\ No newline at end of file