@@ -60,19 +60,61 @@ This file is part of the iText (R) project.
60
60
*/
61
61
public class FlateDecodeFilter implements IFilterHandler {
62
62
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
+
63
104
@ Override
64
105
public byte [] decode (byte [] b , PdfName filterName , PdfObject decodeParams , PdfDictionary streamDictionary ) {
65
106
byte [] res = flateDecode (b , true );
66
- if (res == null )
107
+ if (res == null && ! strictDecoding ) {
67
108
res = flateDecode (b , false );
109
+ }
68
110
b = decodePredictor (res , decodeParams );
69
111
return b ;
70
112
}
71
113
72
114
/**
73
115
* A helper to flateDecode.
74
116
*
75
- * @param in the input data
117
+ * @param in the input data
76
118
* @param strict {@code true} to read a correct stream. {@code false} to try to read a corrupted stream.
77
119
* @return the decoded data
78
120
*/
@@ -89,45 +131,44 @@ public static byte[] flateDecode(byte[] in, boolean strict) {
89
131
zip .close ();
90
132
out .close ();
91
133
return out .toByteArray ();
92
- }
93
- catch (Exception e ) {
134
+ } catch (Exception e ) {
94
135
if (strict )
95
136
return null ;
96
137
return out .toByteArray ();
97
138
}
98
139
}
99
140
100
141
/**
101
- * @param in Input byte array.
142
+ * @param in Input byte array.
102
143
* @param decodeParams PdfDictionary of decodeParams.
103
144
* @return a byte array
104
145
*/
105
146
public static byte [] decodePredictor (byte [] in , PdfObject decodeParams ) {
106
147
if (decodeParams == null || decodeParams .getType () != PdfObject .DICTIONARY )
107
148
return in ;
108
- PdfDictionary dic = (PdfDictionary )decodeParams ;
149
+ PdfDictionary dic = (PdfDictionary ) decodeParams ;
109
150
PdfObject obj = dic .get (PdfName .Predictor );
110
151
if (obj == null || obj .getType () != PdfObject .NUMBER )
111
152
return in ;
112
- int predictor = ((PdfNumber )obj ).intValue ();
153
+ int predictor = ((PdfNumber ) obj ).intValue ();
113
154
if (predictor < 10 && predictor != 2 )
114
155
return in ;
115
156
int width = 1 ;
116
157
obj = dic .get (PdfName .Columns );
117
158
if (obj != null && obj .getType () == PdfObject .NUMBER )
118
- width = ((PdfNumber )obj ).intValue ();
159
+ width = ((PdfNumber ) obj ).intValue ();
119
160
int colors = 1 ;
120
161
obj = dic .get (PdfName .Colors );
121
162
if (obj != null && obj .getType () == PdfObject .NUMBER )
122
- colors = ((PdfNumber )obj ).intValue ();
163
+ colors = ((PdfNumber ) obj ).intValue ();
123
164
int bpc = 8 ;
124
165
obj = dic .get (PdfName .BitsPerComponent );
125
166
if (obj != null && obj .getType () == PdfObject .NUMBER )
126
- bpc = ((PdfNumber )obj ).intValue ();
167
+ bpc = ((PdfNumber ) obj ).intValue ();
127
168
DataInputStream dataStream = new DataInputStream (new ByteArrayInputStream (in ));
128
169
ByteArrayOutputStream fout = new ByteArrayOutputStream (in .length );
129
170
int bytesPerPixel = colors * bpc / 8 ;
130
- int bytesPerRow = (colors * width * bpc + 7 )/ 8 ;
171
+ int bytesPerRow = (colors * width * bpc + 7 ) / 8 ;
131
172
byte [] curr = new byte [bytesPerRow ];
132
173
byte [] prior = new byte [bytesPerRow ];
133
174
if (predictor == 2 ) {
@@ -136,7 +177,7 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
136
177
for (int row = 0 ; row < numRows ; row ++) {
137
178
int rowStart = row * bytesPerRow ;
138
179
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 ]);
140
181
}
141
182
}
142
183
}
@@ -174,7 +215,7 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
174
215
curr [i ] += (byte ) (prior [i ] / 2 );
175
216
}
176
217
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 );
178
219
}
179
220
break ;
180
221
case 4 : //PNG_FILTER_PAETH
@@ -201,7 +242,7 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
201
242
} else {
202
243
ret = c ;
203
244
}
204
- curr [i ] += (byte )ret ;
245
+ curr [i ] += (byte ) ret ;
205
246
}
206
247
break ;
207
248
default :
@@ -210,10 +251,9 @@ public static byte[] decodePredictor(byte[] in, PdfObject decodeParams) {
210
251
}
211
252
try {
212
253
fout .write (curr );
213
- }
214
- catch (IOException ioe ) {
254
+ } catch (IOException ioe ) {
215
255
// Never happens
216
- assert true : "Happens!" ;
256
+ assert true : "Happens!" ;
217
257
}
218
258
219
259
// Swap curr and prior
0 commit comments