OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | 7 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
8 | 8 |
9 #include <limits.h> | 9 #include <limits.h> |
| 10 |
| 11 #include <algorithm> |
10 #include <utility> | 12 #include <utility> |
11 #include <vector> | 13 #include <vector> |
12 | 14 |
13 #include "core/fpdfapi/fpdf_parser/fpdf_parser_utility.h" | 15 #include "core/fpdfapi/fpdf_parser/fpdf_parser_utility.h" |
14 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 16 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
15 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 17 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
16 #include "core/fpdfapi/include/cpdf_modulemgr.h" | 18 #include "core/fpdfapi/include/cpdf_modulemgr.h" |
17 #include "core/fxcodec/include/fx_codec.h" | 19 #include "core/fxcodec/include/fx_codec.h" |
18 #include "core/fxcrt/include/fx_ext.h" | 20 #include "core/fxcrt/include/fx_ext.h" |
19 #include "third_party/base/stl_util.h" | 21 #include "third_party/base/stl_util.h" |
20 | 22 |
21 #define _STREAM_MAX_SIZE_ 20 * 1024 * 1024 | 23 namespace { |
| 24 |
| 25 const uint32_t kMaxStreamSize = 20 * 1024 * 1024; |
| 26 |
| 27 bool CheckFlateDecodeParams(int Colors, int BitsPerComponent, int Columns) { |
| 28 if (Colors < 0 || BitsPerComponent < 0 || Columns < 0) |
| 29 return false; |
| 30 |
| 31 int check = Columns; |
| 32 if (check > 0 && Colors > INT_MAX / check) |
| 33 return false; |
| 34 |
| 35 check *= Colors; |
| 36 if (check > 0 && BitsPerComponent > INT_MAX / check) |
| 37 return false; |
| 38 |
| 39 return check * BitsPerComponent <= INT_MAX - 7; |
| 40 } |
| 41 |
| 42 } // namespace |
22 | 43 |
23 const uint16_t PDFDocEncoding[256] = { | 44 const uint16_t PDFDocEncoding[256] = { |
24 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, | 45 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, |
25 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, | 46 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, |
26 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x02d8, 0x02c7, 0x02c6, | 47 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x02d8, 0x02c7, 0x02c6, |
27 0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc, 0x0020, 0x0021, 0x0022, 0x0023, | 48 0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc, 0x0020, 0x0021, 0x0022, 0x0023, |
28 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, | 49 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, |
29 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, | 50 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, |
30 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, | 51 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, |
31 0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | 52 0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 if (!bFirst) | 185 if (!bFirst) |
165 dest_size++; | 186 dest_size++; |
166 return i; | 187 return i; |
167 } | 188 } |
168 | 189 |
169 uint32_t RunLengthDecode(const uint8_t* src_buf, | 190 uint32_t RunLengthDecode(const uint8_t* src_buf, |
170 uint32_t src_size, | 191 uint32_t src_size, |
171 uint8_t*& dest_buf, | 192 uint8_t*& dest_buf, |
172 uint32_t& dest_size) { | 193 uint32_t& dest_size) { |
173 uint32_t i = 0; | 194 uint32_t i = 0; |
174 uint32_t old; | |
175 dest_size = 0; | 195 dest_size = 0; |
176 while (i < src_size) { | 196 while (i < src_size) { |
| 197 if (src_buf[i] == 128) |
| 198 break; |
| 199 |
| 200 uint32_t old = dest_size; |
177 if (src_buf[i] < 128) { | 201 if (src_buf[i] < 128) { |
178 old = dest_size; | |
179 dest_size += src_buf[i] + 1; | 202 dest_size += src_buf[i] + 1; |
180 if (dest_size < old) | 203 if (dest_size < old) |
181 return FX_INVALID_OFFSET; | 204 return FX_INVALID_OFFSET; |
182 i += src_buf[i] + 2; | 205 i += src_buf[i] + 2; |
183 } else if (src_buf[i] > 128) { | 206 } else { |
184 old = dest_size; | |
185 dest_size += 257 - src_buf[i]; | 207 dest_size += 257 - src_buf[i]; |
186 if (dest_size < old) | 208 if (dest_size < old) |
187 return FX_INVALID_OFFSET; | 209 return FX_INVALID_OFFSET; |
188 i += 2; | 210 i += 2; |
189 } else { | |
190 break; | |
191 } | 211 } |
192 } | 212 } |
193 if (dest_size >= _STREAM_MAX_SIZE_) | 213 if (dest_size >= kMaxStreamSize) |
194 return FX_INVALID_OFFSET; | 214 return FX_INVALID_OFFSET; |
| 215 |
195 dest_buf = FX_Alloc(uint8_t, dest_size); | 216 dest_buf = FX_Alloc(uint8_t, dest_size); |
196 i = 0; | 217 i = 0; |
197 int dest_count = 0; | 218 int dest_count = 0; |
198 while (i < src_size) { | 219 while (i < src_size) { |
| 220 if (src_buf[i] == 128) |
| 221 break; |
| 222 |
199 if (src_buf[i] < 128) { | 223 if (src_buf[i] < 128) { |
200 uint32_t copy_len = src_buf[i] + 1; | 224 uint32_t copy_len = src_buf[i] + 1; |
201 uint32_t buf_left = src_size - i - 1; | 225 uint32_t buf_left = src_size - i - 1; |
202 if (buf_left < copy_len) { | 226 if (buf_left < copy_len) { |
203 uint32_t delta = copy_len - buf_left; | 227 uint32_t delta = copy_len - buf_left; |
204 copy_len = buf_left; | 228 copy_len = buf_left; |
205 FXSYS_memset(dest_buf + dest_count + copy_len, '\0', delta); | 229 FXSYS_memset(dest_buf + dest_count + copy_len, '\0', delta); |
206 } | 230 } |
207 FXSYS_memcpy(dest_buf + dest_count, src_buf + i + 1, copy_len); | 231 FXSYS_memcpy(dest_buf + dest_count, src_buf + i + 1, copy_len); |
208 dest_count += src_buf[i] + 1; | 232 dest_count += src_buf[i] + 1; |
209 i += src_buf[i] + 2; | 233 i += src_buf[i] + 2; |
210 } else if (src_buf[i] > 128) { | 234 } else { |
211 int fill = 0; | 235 int fill = 0; |
212 if (i < src_size - 1) { | 236 if (i < src_size - 1) { |
213 fill = src_buf[i + 1]; | 237 fill = src_buf[i + 1]; |
214 } | 238 } |
215 FXSYS_memset(dest_buf + dest_count, fill, 257 - src_buf[i]); | 239 FXSYS_memset(dest_buf + dest_count, fill, 257 - src_buf[i]); |
216 dest_count += 257 - src_buf[i]; | 240 dest_count += 257 - src_buf[i]; |
217 i += 2; | 241 i += 2; |
218 } else { | |
219 break; | |
220 } | 242 } |
221 } | 243 } |
222 uint32_t ret = i + 1; | 244 |
223 if (ret > src_size) { | 245 return std::min(i + 1, src_size); |
224 ret = src_size; | |
225 } | |
226 return ret; | |
227 } | 246 } |
228 | 247 |
229 CCodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( | 248 CCodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( |
230 const uint8_t* src_buf, | 249 const uint8_t* src_buf, |
231 uint32_t src_size, | 250 uint32_t src_size, |
232 int width, | 251 int width, |
233 int height, | 252 int height, |
234 const CPDF_Dictionary* pParams) { | 253 const CPDF_Dictionary* pParams) { |
235 int K = 0; | 254 int K = 0; |
236 FX_BOOL EndOfLine = FALSE; | 255 bool EndOfLine = false; |
237 FX_BOOL ByteAlign = FALSE; | 256 bool ByteAlign = false; |
238 FX_BOOL BlackIs1 = FALSE; | 257 bool BlackIs1 = false; |
239 int Columns = 1728; | 258 int Columns = 1728; |
240 int Rows = 0; | 259 int Rows = 0; |
241 if (pParams) { | 260 if (pParams) { |
242 K = pParams->GetIntegerFor("K"); | 261 K = pParams->GetIntegerFor("K"); |
243 EndOfLine = pParams->GetIntegerFor("EndOfLine"); | 262 EndOfLine = !!pParams->GetIntegerFor("EndOfLine"); |
244 ByteAlign = pParams->GetIntegerFor("EncodedByteAlign"); | 263 ByteAlign = !!pParams->GetIntegerFor("EncodedByteAlign"); |
245 BlackIs1 = pParams->GetIntegerFor("BlackIs1"); | 264 BlackIs1 = !!pParams->GetIntegerFor("BlackIs1"); |
246 Columns = pParams->GetIntegerFor("Columns", 1728); | 265 Columns = pParams->GetIntegerFor("Columns", 1728); |
247 Rows = pParams->GetIntegerFor("Rows"); | 266 Rows = pParams->GetIntegerFor("Rows"); |
248 if (Rows > USHRT_MAX) { | 267 if (Rows > USHRT_MAX) { |
249 Rows = 0; | 268 Rows = 0; |
250 } | 269 } |
251 } | 270 } |
252 return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder( | 271 return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder( |
253 src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1, | 272 src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1, |
254 Columns, Rows); | 273 Columns, Rows); |
255 } | 274 } |
256 | 275 |
257 static FX_BOOL CheckFlateDecodeParams(int Colors, | |
258 int BitsPerComponent, | |
259 int Columns) { | |
260 if (Columns < 0) { | |
261 return FALSE; | |
262 } | |
263 int check = Columns; | |
264 if (Colors < 0 || (check > 0 && Colors > INT_MAX / check)) { | |
265 return FALSE; | |
266 } | |
267 check *= Colors; | |
268 if (BitsPerComponent < 0 || | |
269 (check > 0 && BitsPerComponent > INT_MAX / check)) { | |
270 return FALSE; | |
271 } | |
272 check *= BitsPerComponent; | |
273 if (check > INT_MAX - 7) { | |
274 return FALSE; | |
275 } | |
276 return TRUE; | |
277 } | |
278 | |
279 CCodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder( | 276 CCodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder( |
280 const uint8_t* src_buf, | 277 const uint8_t* src_buf, |
281 uint32_t src_size, | 278 uint32_t src_size, |
282 int width, | 279 int width, |
283 int height, | 280 int height, |
284 int nComps, | 281 int nComps, |
285 int bpc, | 282 int bpc, |
286 const CPDF_Dictionary* pParams) { | 283 const CPDF_Dictionary* pParams) { |
287 int predictor = 0; | 284 int predictor = 0; |
288 int Colors = 0, BitsPerComponent = 0, Columns = 0; | 285 int Colors = 0, BitsPerComponent = 0, Columns = 0; |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 uint32_t src_size, | 566 uint32_t src_size, |
570 uint8_t*& dest_buf, | 567 uint8_t*& dest_buf, |
571 uint32_t& dest_size) { | 568 uint32_t& dest_size) { |
572 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); | 569 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); |
573 if (pEncoders) { | 570 if (pEncoders) { |
574 return pEncoders->GetFlateModule()->FlateOrLZWDecode( | 571 return pEncoders->GetFlateModule()->FlateOrLZWDecode( |
575 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size); | 572 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size); |
576 } | 573 } |
577 return 0; | 574 return 0; |
578 } | 575 } |
OLD | NEW |