You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by pk...@apache.org on 2009/03/17 10:36:38 UTC

svn commit: r755167 - /incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/filter/FlateFilter.java

Author: pkoch
Date: Tue Mar 17 09:36:38 2009
New Revision: 755167

URL: http://svn.apache.org/viewvc?rev=755167&view=rev
Log:
PDFBOX-438: FlateFilter: endless loop because of missing length check (for encrypted pdfs)

Modified:
    incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/filter/FlateFilter.java

Modified: incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/filter/FlateFilter.java
URL: http://svn.apache.org/viewvc/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/filter/FlateFilter.java?rev=755167&r1=755166&r2=755167&view=diff
==============================================================================
--- incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/filter/FlateFilter.java (original)
+++ incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/filter/FlateFilter.java Tue Mar 17 09:36:38 2009
@@ -89,58 +89,60 @@
             decompressor = new InflaterInputStream(compressedData);
             int amountRead;
             int mayRead = compressedData.available();
-            byte[] buffer = new byte[Math.min(mayRead,BUFFER_SIZE)];
 
-            // Decode data using given predictor
-            if (predictor==-1 || predictor == 1 || predictor == 10)
-            {
-                // decoding not needed
-                while ((amountRead = decompressor.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1)
-                {
-                    result.write(buffer, 0, amountRead);
-                }
-            }
-            else
-            {
-                if( colors==-1 )
-                {
-                    throw new IOException("Error: Could not read 'colors' attribute to decompress flate stream.");
-                }
-                if( bitsPerPixel==-1 )
-                {
-                    throw new IOException("Error: Could not read 'bitsPerPixel' attribute to decompress flate stream.");
-                }
-                if( columns==-1 )
-                {
-                    throw new IOException("Error: Could not read 'columns' attribute to decompress flate stream.");
-                }
+            if (mayRead > 0) {
+                byte[] buffer = new byte[Math.min(mayRead,BUFFER_SIZE)];
 
-                baos = new ByteArrayOutputStream();
-                while ((amountRead = decompressor.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1)
+                // Decode data using given predictor
+                if (predictor==-1 || predictor == 1 || predictor == 10)
                 {
-                    baos.write(buffer, 0, amountRead);
+                    // decoding not needed
+                    while ((amountRead = decompressor.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1)
+                    {
+                        result.write(buffer, 0, amountRead);
+                    }
                 }
-                baos.flush();
+                else
+                {
+                    if( colors==-1 )
+                    {
+                        throw new IOException("Error: Could not read 'colors' attribute to decompress flate stream.");
+                    }
+                    if( bitsPerPixel==-1 )
+                    {
+                        throw new IOException("Error: Could not read 'bitsPerPixel' attribute to decompress flate stream.");
+                    }
+                    if( columns==-1 )
+                    {
+                        throw new IOException("Error: Could not read 'columns' attribute to decompress flate stream.");
+                    }
 
-                // Copy data to ByteArrayInputStream for reading
-                bais = new ByteArrayInputStream(baos.toByteArray());
-                baos.close();
-                baos = null;
+                    baos = new ByteArrayOutputStream();
+                    while ((amountRead = decompressor.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1)
+                    {
+                        baos.write(buffer, 0, amountRead);
+                    }
+                    baos.flush();
 
-                byte[] decodedData = decodePredictor(predictor, colors, bitsPerPixel, columns, bais);
-                bais.close();
-                bais = new ByteArrayInputStream(decodedData);
+                    // Copy data to ByteArrayInputStream for reading
+                    bais = new ByteArrayInputStream(baos.toByteArray());
+                    baos.close();
+                    baos = null;
+
+                    byte[] decodedData = decodePredictor(predictor, colors, bitsPerPixel, columns, bais);
+                    bais.close();
+                    bais = new ByteArrayInputStream(decodedData);
 
-                // write decoded data to result
-                while ((amountRead = bais.read(buffer)) != -1)
-                {
-                    result.write(buffer, 0, amountRead);
+                    // write decoded data to result
+                    while ((amountRead = bais.read(buffer)) != -1)
+                    {
+                        result.write(buffer, 0, amountRead);
+                    }
+                    bais.close();
+                    bais = null;
                 }
-                bais.close();
-                bais = null;
             }
 
-
             result.flush();
         }
         finally