@@ -60,19 +60,61 @@ This file is part of the iText (R) project.
6060 */
6161public class FlateDecodeFilter implements IFilterHandler {
6262
63+ /**
64+ * Defines how the corrupted streams should be treated.
65+ */
66+ private boolean strictDecoding = false ;
67+
68+ /**
69+ * Creates a FlateDecodeFilter.
70+ */
71+ public FlateDecodeFilter () {
72+ this (false );
73+ }
74+
75+ /**
76+ * Creates a FlateDecodeFilter.
77+ *
78+ * @param strictDecoding defines whether the decoder will try to read a corrupted stream
79+ */
80+ public FlateDecodeFilter (boolean strictDecoding ) {
81+ this .strictDecoding = strictDecoding ;
82+ }
83+
84+ /**
85+ * Checks whether the decoder will try to read a corrupted stream (not strict) or not (strict)
86+ *
87+ * @return true if the decoder will try to read a corrupted stream otherwise false
88+ */
89+ public boolean isStrictDecoding () {
90+ return strictDecoding ;
91+ }
92+
93+ /**
94+ * Defines how the corrupted streams should be treated.
95+ *
96+ * @param strict true if the decoder should try to read a corrupted stream otherwise false
97+ * @return the decoder
98+ */
99+ public FlateDecodeFilter setStrictDecoding (boolean strict ) {
100+ this .strictDecoding = strict ;
101+ return this ;
102+ }
103+
63104 @ Override
64105 public byte [] decode (byte [] b , PdfName filterName , PdfObject decodeParams , PdfDictionary streamDictionary ) {
65106 byte [] res = flateDecode (b , true );
66- if (res == null )
107+ if (res == null && ! strictDecoding ) {
67108 res = flateDecode (b , false );
109+ }
68110 b = decodePredictor (res , decodeParams );
69111 return b ;
70112 }
71113
72114 /**
73115 * A helper to flateDecode.
74116 *
75- * @param in the input data
117+ * @param in the input data
76118 * @param strict {@code true} to read a correct stream. {@code false} to try to read a corrupted stream.
77119 * @return the decoded data
78120 */
@@ -89,45 +131,44 @@ public static byte[] flateDecode(byte[] in, boolean strict) {
89131 zip .close ();
90132 out .close ();
91133 return out .toByteArray ();
92- }
93- catch (Exception e ) {
134+ } catch (Exception e ) {
94135 if (strict )
95136 return null ;
96137 return out .toByteArray ();
97138 }
98139 }
99140
100141 /**
101- * @param in Input byte array.
142+ * @param in Input byte array.
102143 * @param decodeParams PdfDictionary of decodeParams.
103144 * @return a byte array
104145 */
105146 public static byte [] decodePredictor (byte [] in , PdfObject decodeParams ) {
106147 if (decodeParams == null || decodeParams .getType () != PdfObject .DICTIONARY )
107148 return in ;
108- PdfDictionary dic = (PdfDictionary )decodeParams ;
149+ PdfDictionary dic = (PdfDictionary ) decodeParams ;
109150 PdfObject obj = dic .get (PdfName .Predictor );
110151 if (obj == null || obj .getType () != PdfObject .NUMBER )
111152 return in ;
112- int predictor = ((PdfNumber )obj ).intValue ();
153+ int predictor = ((PdfNumber ) obj ).intValue ();
113154 if (predictor < 10 && predictor != 2 )
114155 return in ;
115156 int width = 1 ;
116157 obj = dic .get (PdfName .Columns );
117158 if (obj != null && obj .getType () == PdfObject .NUMBER )
118- width = ((PdfNumber )obj ).intValue ();
159+ width = ((PdfNumber ) obj ).intValue ();
119160 int colors = 1 ;
120161 obj = dic .get (PdfName .Colors );
121162 if (obj != null && obj .getType () == PdfObject .NUMBER )
122- colors = ((PdfNumber )obj ).intValue ();
163+ colors = ((PdfNumber ) obj ).intValue ();
123164 int bpc = 8 ;
124165 obj = dic .get (PdfName .BitsPerComponent );
125166 if (obj != null && obj .getType () == PdfObject .NUMBER )
126- bpc = ((PdfNumber )obj ).intValue ();
167+ bpc = ((PdfNumber ) obj ).intValue ();
127168 DataInputStream dataStream = new DataInputStream (new ByteArrayInputStream (in ));
128169 ByteArrayOutputStream fout = new ByteArrayOutputStream (in .length );
129170 int bytesPerPixel = colors * bpc / 8 ;
130- int bytesPerRow = (colors * width * bpc + 7 )/ 8 ;
171+ int bytesPerRow = (colors * width * bpc + 7 ) / 8 ;
131172 byte [] curr = new byte [bytesPerRow ];
132173 byte [] prior = new byte [bytesPerRow ];
133174 if (predictor == 2 ) {
@@ -136,7 +177,7 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
136177 for (int row = 0 ; row < numRows ; row ++) {
137178 int rowStart = row * bytesPerRow ;
138179 for (int col = bytesPerPixel ; col < bytesPerRow ; col ++) {
139- in [rowStart + col ] = (byte )(in [rowStart + col ] + in [rowStart + col - bytesPerPixel ]);
180+ in [rowStart + col ] = (byte ) (in [rowStart + col ] + in [rowStart + col - bytesPerPixel ]);
140181 }
141182 }
142183 }
@@ -174,7 +215,7 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
174215 curr [i ] += (byte ) (prior [i ] / 2 );
175216 }
176217 for (int i = bytesPerPixel ; i < bytesPerRow ; i ++) {
177- curr [i ] += (byte ) (((curr [i - bytesPerPixel ] & 0xff ) + (prior [i ] & 0xff ))/ 2 );
218+ curr [i ] += (byte ) (((curr [i - bytesPerPixel ] & 0xff ) + (prior [i ] & 0xff )) / 2 );
178219 }
179220 break ;
180221 case 4 : //PNG_FILTER_PAETH
@@ -201,7 +242,7 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
201242 } else {
202243 ret = c ;
203244 }
204- curr [i ] += (byte )ret ;
245+ curr [i ] += (byte ) ret ;
205246 }
206247 break ;
207248 default :
@@ -210,10 +251,9 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
210251 }
211252 try {
212253 fout .write (curr );
213- }
214- catch (IOException ioe ) {
254+ } catch (IOException ioe ) {
215255 // Never happens
216- assert true : "Happens!" ;
256+ assert true : "Happens!" ;
217257 }
218258
219259 // Swap curr and prior
0 commit comments