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:44:26 UTC
svn commit: r953076 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: cos/COSName.java
filter/CryptFilter.java filter/FilterManager.java filter/IdentityFilter.java
Author: lehmi
Date: Wed Jun 9 16:44:26 2010
New Revision: 953076
URL: http://svn.apache.org/viewvc?rev=953076&view=rev
Log:
PDFBOX-730: added the default implementation for a crypt filter. Patch by Adam Nichols (adam at swmc dot com)
Added:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/CryptFilter.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/IdentityFilter.java
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FilterManager.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=953076&r1=953075&r2=953076&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:44:26 2010
@@ -242,6 +242,10 @@ public final class COSName extends COSBa
*/
public static final COSName CROP_BOX = new COSName( "CropBox" );
/**
+ * The Crypt filter.
+ */
+ public static final COSName CRYPT = new COSName( "Crypt" );
+ /**
* A common COSName value.
*/
public static final COSName CS = new COSName( "CS" );
@@ -473,6 +477,10 @@ public final class COSName extends COSBa
public static final COSName ID_TREE = new COSName("IDTree");
/**
+ * A common COSName value.
+ */
+ public static final COSName IDENTITY = new COSName( "Identity" );
+ /**
* A common COSName value.
*/
public static final COSName IDENTITY_H = new COSName( "Identity-H" );
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/CryptFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/CryptFilter.java?rev=953076&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/CryptFilter.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/CryptFilter.java Wed Jun 9 16:44:26 2010
@@ -0,0 +1,68 @@
+/*
+ * 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.filter;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+
+/**
+ *
+ * @author adam.nichols
+ */
+public class CryptFilter implements Filter
+{
+ /**
+ * {@inheritDoc}
+ */
+ public void decode( InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex )
+ throws IOException
+ {
+ COSName encryptionName = (COSName)options.getDictionaryObject(COSName.NAME);
+ if(encryptionName == null || encryptionName.equals(COSName.IDENTITY))
+ {
+ // currently the only supported implementation is the Identity crypt filter
+ Filter identityFilter = new IdentityFilter();
+ identityFilter.decode(compressedData, result, options, filterIndex);
+ }
+ else
+ {
+ throw new IOException("Unsupported crypt filter "+encryptionName.getName());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void encode( InputStream rawData, OutputStream result, COSDictionary options, int filterIndex )
+ throws IOException
+ {
+ COSName encryptionName = (COSName)options.getDictionaryObject(COSName.NAME);
+ if(encryptionName == null || encryptionName.equals(COSName.IDENTITY))
+ {
+ // currently the only supported implementation is the Identity crypt filter
+ Filter identityFilter = new IdentityFilter();
+ identityFilter.encode(rawData, result, options, filterIndex);
+ }
+ else
+ {
+ throw new IOException("Unsupported crypt filter "+encryptionName.getName());
+ }
+ }
+}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FilterManager.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FilterManager.java?rev=953076&r1=953075&r2=953076&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FilterManager.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FilterManager.java Wed Jun 9 16:44:26 2010
@@ -46,6 +46,7 @@ public class FilterManager
Filter asciiHexFilter = new ASCIIHexFilter();
Filter ascii85Filter = new ASCII85Filter();
Filter runLengthFilter = new RunLengthDecodeFilter();
+ Filter cryptFilter = new CryptFilter();
addFilter( COSName.FLATE_DECODE, flateFilter );
addFilter( COSName.FLATE_DECODE_ABBREVIATION, flateFilter );
@@ -61,7 +62,7 @@ public class FilterManager
addFilter( COSName.ASCII85_DECODE_ABBREVIATION, ascii85Filter );
addFilter( COSName.RUN_LENGTH_DECODE, runLengthFilter );
addFilter( COSName.RUN_LENGTH_DECODE_ABBREVIATION, runLengthFilter );
-
+ addFilter( COSName.CRYPT, cryptFilter );
}
/**
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/IdentityFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/IdentityFilter.java?rev=953076&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/IdentityFilter.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/IdentityFilter.java Wed Jun 9 16:44:26 2010
@@ -0,0 +1,64 @@
+/*
+ * 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.filter;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import org.apache.pdfbox.cos.COSDictionary;
+
+/**
+ * The IdentityFilter filter just passes the data through without any modifications.
+ * This is defined in section 7.6.5 of the PDF 1.7 spec and also stated in table
+ * 26.
+ *
+ * @author adam.nichols
+ */
+public class IdentityFilter implements Filter
+{
+ private static final int BUFFER_SIZE = 1024;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void decode( InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex )
+ throws IOException
+ {
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int amountRead = 0;
+ while( (amountRead = compressedData.read( buffer, 0, BUFFER_SIZE )) != -1 )
+ {
+ result.write( buffer, 0, amountRead );
+ }
+ result.flush();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void encode( InputStream rawData, OutputStream result, COSDictionary options, int filterIndex )
+ throws IOException
+ {
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int amountRead = 0;
+ while( (amountRead = rawData.read( buffer, 0, BUFFER_SIZE )) != -1 )
+ {
+ result.write( buffer, 0, amountRead );
+ }
+ result.flush();
+ }
+}